vrod

vrod

Clarification on ordered for Task.async_stream

I have another question about concurrency in Elixir I hope someone can help me understand. I have been studying the Task module. I am reading the documentation for the Task.async_stream/5 https://hexdocs.pm/elixir/Task.html#async_stream/5
but I am confused by the ordered option. I think I understand what it does – if it is false then tasks a, b, and c may be returned in some different order like b, c, a.

However, I am struggling to understand these sentences: “This option is useful when you have large streams and don’t want to buffer results before they are delivered. This is also useful when you’re using the tasks for side effects.”

Is that saying that using ordered: false is useful when you have large streams? And should I use ordered: false if I am only using the task for side effects?

I think that makes sense, but I do not trust my own understanding with this yet.

Thank you for clarifying!

Most Liked

John-Goff

John-Goff

PR submitted! Thanks for the encouragement :smile: https://github.com/elixir-lang/elixir/pull/10359

John-Goff

John-Goff

I think it could be better worded as “when you’re using tasks only for the side effects”. The ordered key will not make side effects occur in the order of the enumerable, it’ll only affect the returned stream. Side effects will always occur when the task is processed, so if you need to rely on side effect order you should use Enum.each or Enum.map. As you point out, with ordered: false, there’s no need to buffer the results. So the docs are saying basically if you’re going to pipe Task.async_stream into Enum.each, you should use ordered: false.

gregvaughn

gregvaughn

I don’t know why they mention side effects in the docs. Seems to me those could be sensitive to ordering. It really all depends on your algorithm whether the order of the results is important to preserve.

If you have ordered: true but the tasks complete in order of b, a, c, then the internal logic has to buffer the result of b, wait for a to finish, send a’s result downstream, then it sends the buffered b’s response, etc.

If your algorithm can operate correctly with ordered: false I would recommend it, no matter the size of the stream or side-effects.

josevalim

josevalim

Creator of Elixir

Both of your points are great, PR to the docs are appreciated. :slight_smile:

John-Goff

John-Goff

I’ve never had the chance to submit a pr to the docs, this seems like a great opportunity! I’ll get on that today :smile:

Where Next?

Popular in Questions Top

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
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
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
hariharasudhan94
I would like to know what is the best IDE for elixir development?
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
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New

Other popular topics Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
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
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
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
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
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
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement