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 Task — Elixir v1.20.2
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

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.

Where Next?

Popular in Questions Top

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36689 110
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44608 311
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New

We're in Beta

About us Mission Statement