actor

actor

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 https://stackoverflow.com/a/19033882/492293

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?.

Thanks.

Most Liked

al2o3cr

al2o3cr

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

https://hamidreza-s.github.io/erlang/scheduling/real-time/preemptive/migration/2016/02/09/erlang-scheduler-details.html

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).

NobbZ

NobbZ

There are no coroutines in elixir.

dimitarvp

dimitarvp

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.

Where Next?

Popular in Questions Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
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
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
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
Exadra37
Sometimes I want to check if the input into a function is not a blank string. My first approach: defmodule Example do def do_stuff(s...
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
New

Other popular topics Top

sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
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
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
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
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

We're in Beta

About us Mission Statement