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
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
Also Liked
ndrean
Popular in Questions
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
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
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
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
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
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
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
I have an umbrella app.
Some of the apps inside depend on other apps in the umbrella, unsurprisingly.
I’m writing a test for one of the...
New
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
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
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
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
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
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









