I want to simulate an async call to a service where I instantiate some object but don’t know when it is really created. It can be easily above 20s.
I want to run a Task.async
and then keep trying Task.yield
while the later returns nil
.
I tried the code below, with a recursion, to achieve this given the standard timeout: 5000
. Curiously, the first recursion works (9000<2*5000) but the second fails.
Any experience?
I can do:
iex> t = Task.async(fn -> Process.sleep(18_000) end)
iex> Task.yield(t)
nil
iex> Task.yield(t)
nil
iex>Task.yield(t)
{:ok, :ok}
but this fails:
iex> Test.yield_recursion(9_000)
recursion
{:ok, :success}
iex> Test.yield_recursion(11_000)
recursion
nil
with:
def yield_recursion(time) do
async_task =
fn ->
Process.sleep(time)
:success
end
|> Task.async()
case Task.yield(async_task) do
nil ->
IO.puts("recursion")
Task.yield(async_task)
res ->
IO.inspect(res)
end
end