vfsoraki

vfsoraki

I am trying to do some works concurrently. The current code speaks for itself:

  @sources
  |> Flow.from_enumerable(max_demand: 50, min_demand: 20)
  |> Flow.map(fn {mod, fun, args} = mfa ->
    Logger.debug(fn -> ~s(Running #{inspect(mfa)}) end)
  end)
  |> Stream.run()

Basically, I am trying to run a map operation in parallel.

Some questions:

  • Does this map @sources in parallel using 50 workers?
  • Is this the right way to use Flow? I read the docs, and they mentioned partitioning to achieve better performance, but I don’t quite get if it is useful in my case.
  • Is there any other mature library for this kind of tasks? I am already using GenStage so don’t mention it. I think it’s an overkill to start GenStage for some temporary, periodic work (Am I wrong?).

Showing Posts 1 to 5

dimitarvp

dimitarvp

I don’t know how complex your mapping function is but to me Flow and even GenStage seem to be an overkill for the simplified scenario that you presented. Especially GenStage is basically only necessary when you want to have back-pressure (namely consumers to be able to inform the producers that they are giving them work too fast).

Have you tried with Task.async_stream and controlling its maximum concurrency?

vfsoraki

vfsoraki OP

Nothing too complex, some ordinary HTML fetch and parse thing.

How come I have never seen Task.async_stream? Seems like exactly what I needed.

Anyway, what I did with Flow at first was wrong, here is a version that works in parallel:

  @sources
  |> Flow.from_enumerable()
  |> Flow.partition(stages: 20)
  |> Flow.map(fn {mod, fun, args} = mfa ->
    Logger.debug(fn -> ~s(Running #{inspect(mfa)}) end)
  end)
  |> Stream.run()

I know, 20 stages are a bit high, but my work is mostly IO bound.

vfsoraki

vfsoraki OP

Just for the sake of it, this is the same thing using Task:

  MySupervisor
  |> Task.Supervisor.async_stream_nolink(@sources, fn {mod, fun, args} = mfa ->
    Logger.debug(fn -> ~s(Running #{inspect(mfa)}) end)
  end, max_concurrency: 20, timeout: 120_000, ordered: false, on_timeout: :kill_task)
  |> Stream.run()

I used supervisor and no linking version, because tasks could fail sometimes.

But I think the Flow is more beautiful, and since I’m already using GenStage somewhere else (processing infinite stream of data), having Flow wouldn’t hurt that much.

One thing about Task.Supervisor.async_stream_nolink bothers me, why arguments start with supervisor name and not the enumerable? Seems unnatural.

dimitarvp

dimitarvp

Oh well, it can happen. :slight_smile: Important thing is you know about it now!

If that is indeed the case then you are better off using Task.async_stream and manually tune its concurrency parameter to see how much I/O plus CPU load your work can tolerate. IMO if you’re mainly I/O bound then using the default behaviour of Task.async_stream will leave your CPU under-utilised so definitely use a higher concurrency!


Recently I was exercising importing CSV data into a DB from a public dataset. And I actually had to increase my DB pool from 10 to 20 (my CPU’s virtual core count is exactly 20), and instead of putting records one by one in the DB, I used chunking and then inserting records in those chunks (I chose 500 at a time). And this was multiplexed over all CPU cores and I was able to achieve about 90% load on all CPU cores.

In short, Elixir gives you everything you might need for your work but you have to make sure you properly load your machine.

ityonemo

ityonemo

Because the module is Task.Supervisor. The convention in Elixir is that if you are calling a module function, then the first parameter will be whatever data structure or thing is referred to by the module (of course Elixir is not perfect). This was a major bone of contention with erlang, which, quite frankly is all over the place and there are several erlang functions which I am constantly looking up the docs on and shaking my head.

— All posts loaded —

Where Next? Top

Trending in Questions Top

Hellowman
Hello everyone! I am new to coding with Elixir/Erlang. And i am looking for some utilities for fuzzing testing like AFL++. Does anyone ...
New
yarrichar
I’m very new to Elixir, so sorry if this is a silly question: I am wondering what happens to the mailbox of a process when the machine i...
New
pragdave
How do I tell Hologram not to escape HTML when substituting into templates? {Textile.render(@product.long_description)} escapes the HTM...
New
Fl4m3Ph03n1x
Background When trying to execute mix release on a Windows 11 machine for a Phoenix project I get the following error: * assembling mark...
New
mandar1045
I load tested a plain Phoenix chat app backed by Postgres with 100 million messages in it, everything on default settings, to see what br...
New
rlipscombe
I’d like to create a file with 600 permissions (rw-------), but there’s a race condition if I create it and then change the permissions. ...
New
saleyn
Is there a way to import record definitions in the iex shell for pretty printing (similar to the rr command in the erl shell)? This woul...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
261 13149 112
New
dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
raa-ziq
Hi everyone :waving_hand: I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New
kip
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews