Consider following snippets of code:
a)
IO.puts "test1"
task = Task.async fn ->
Foo.Bar.send_file_for_processing(file_url)
end
IO.puts "test2"
content = Task.await(task)
IO.puts "test4"
b)
IO.puts "test1"
content = Foo.Bar.send_file_for_processing(file_url)
IO.puts "test2"
and in Foo.Bar module:
def send_file_for_processing(file_url) do
json = file_url |> encoded_file |> request_json
IO.puts "test3"
request = HTTPoison.post(
@processing_endpoint,
json,
@headers,
params: %{"key": @api_key}, connect_timeout: 100000, recv_timeout: 100000, timeout: 100000
)
IO.puts "test5"
(...)
end
When I use code snippet a), the "test5" is never reached, as if the program would hang during HTTPoison POST request. It just never finishes. Meanwhile, with snippet b), the HTTPoison POST request completes normally and without any delays.
To be honest, debugging this made me lose some time and I still do not understand the problem with the snippet a). Am I misusing the Task module? I have checked the documentation and could not find anything that would explain this issue to me.
EDIT: Output for snippet a)
test1
test2
test3
I can see you have high timeout values for the HTTP request. The task has a default timeout value of 5_000 ms
, so it's very possible that the task timeouts before the request is finished. Subsequently the task is killed and you never see the output. The Task.await/2
function accepts the timeout as the second argument.
Regardless of that you should be seeing a task timeout errors. Perhaps you're ignoring some output?