kaa.python

kaa.python

Wait for last task or job to finish

I have a bunch of tasks or jobs I can run in parallel. What’s the best to run them and wait for the last one to finish?
I know I can use spawn or Task to run them.

However, how do I wait for the last task to finish?

Marked As Solved

dom

dom

My response to that was “yeah that would work”.

Also Liked

imetallica

imetallica

You can use something like:

[task1, task2, task3] |> Task.yield_many()

From docs: https://hexdocs.pm/elixir/Task.html#yield_many/2

dom

dom

Yeah that would work. If you need bounded retries you can do something recursive like this:

  def call_with_retries(args, attempts \\ 5) do
    case call_some_api(args) do
      {:ok, result} ->
        {:ok, result}
      {:error, _} when attempts > 0 ->
        call_with_retries(args, attempts - 1)
      {:error, reason} ->
        {:error, reason}
    end
  end
dom

dom

Send a message back to the parent process, then have that process wait to receive every response:

parent = self()
refs = Enum.map([1,2,3], fn n ->
  ref = make_ref()
  spawn_link(fn -> Process.sleep(n * 1000); send(parent, {:done, ref}) end)
  ref
end)
Enum.each(refs, fn ref ->
  receive do
    {:done, ref} -> :ok
  end
end)

Where Next?

Popular in Questions Top

joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
makeitrein
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project. Baby step #1 is extracting the number ...
New
Fl4m3Ph03n1x
Background Let’s assume I have a typical GenServer that receives messages as requests, does some operation in a DB and returns responses....
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New

Other popular topics Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
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
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New

We're in Beta

About us Mission Statement