Task.async and recursion

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

The problem is that yield_recursion is not a recursive function. After the second call to Task.yield, it returns.

This would work:

defmodule X do
  def yield_recursion(time) do
    fn ->
      Process.sleep(time)
      :success
    end
    |> Task.async()
    |> _yield_recursion()
  end

  defp _yield_recursion(async_task) do
    case Task.yield(async_task, 100) do
      nil ->
        IO.puts("recursion")
        _yield_recursion(async_task) # function calls itself == recursion

      res ->
        IO.inspect(res)
    end
  end
end

X.yield_recursion(5000)
2 Likes

Yes indeed, blind :slight_smile:

1 Like