ndrean

ndrean

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

Marked As Solved

lud

lud

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)

Also Liked

ndrean

ndrean

Yes indeed, blind :slight_smile:

Where Next?

Popular in Questions Top

ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

Other popular topics Top

New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement