Making Serial Code Concurrent with Tasks
Explore how to convert serial Elixir code into concurrent operations using Tasks. Understand Task.async, Task.await, and Task.async_stream to handle multiple jobs efficiently while managing system resources and concurrency limits for better performance.
We'll cover the following...
The natural go-to concurrency construct for Elixir beginners is the process. Still, we generally want to incorporate proven features that have already addressed the subtle complexities of concurrency. We’ll cover processes here so we can put them in context when the inevitable edge cases do occur.
We’ve already spent some time using Elixir primitives for processes, but it bears repeating. We have access to the primitives to do the following:
-
Send messages with
send/2. -
Spawn processes with the various versions of
spawn/nandspawn_monitor/n.
Typically, we’ll want our processes to be OTP processes because we’ll want to take advantage of the GenServer lifecycle. Elixir can’t manage what it doesn’t know about.
We’ll rarely send messages with the send variants, though we might use some of its close cousins. The following are useful for dealing with various scheduling problems:
-
Process.send_after -
:timer.send_interval...