Use cases of coroutines in elixir

I am new to elixir and also the concept of co-routines. I am trying to warp my head around how we can make use of time slice like explained here

Lets say i have a list with 10,000 items and i want to process the list with coroutines.

Lets say i have 5 coroutines that i am using to process the list. My thinking is that i divide the list of 10,000 between the 5 coroutines and have each coroutine assigned 2,000 items.

I would then use time slice to visit each 2,000 items make some progress jump to the next do the same and do this until each and every item in the groups of 2000 are all visited and processed.

Is this an acceptable use of time slice or wouldn it be faster to just process the 20,000 in one function?.


There are no coroutines in elixir.

1 Like

We don’t need coroutines in Erlang/Elixir because we have Erlang processes. So yes, divide across multiple processes if you have multiple CPU cores available…

Under the hood, the BEAM is already doing a sort of time-slicing:

For your case of a large list, look into Task.async_stream which does something very close to what you’ve described (spins up multiple processes to work through a large list).


Using Elixir builtins like Task.async_stream and the Flow library gives you transparent parallelism / concurrency, and having in mind that the Erlang/Elixir runtime (the BEAM VM) is already preemptively scheduled then you get parallel computations almost for free and with very little syntactic overhead.

I strongly recommend you review both linked docs.

And yep, as others said, Erlang/Elixir already have “coroutines” in the form of processes (not to be confused with OS processes) that are fairly time-sliced, automatically for you.

1 Like

Thanks shall do.

Thanks everyone for the answers. I appreciated