Chapter 1 - Easy Concurrency With the Task Module
-
Multi-tasking is the concept that one can split their attention into more than one or more activities simultaneously; the concurrent execution of multiple tasks (also known as processes) over a certain period of time.
-
Multi-threading is the ability of a central processing unit (CPU) (or a single core in a multi-core processor) to provide multiple threads of execution concurrently, supported by the operating system.
-
Multi-core brought parallelism and allowed tasks to run simultaneously; this architecture enabled concurrency and parallelism by supporting two or more CPUs on a single machine.
First, running code on a multi-core does not automatically make it efficient. The language needs to handle concurrency and parallelism for its own, instead of demands to an external service or solution, and the Erlang Virtual Machine(BEAM) has that for its own.
And it will use the Open Telecom Platform(OTP) Behaviours and Concepts to build concurrent and fault-tolerant applications, recover from failures, use back-pressure to deal with limited system resources, and how to handle errors, and prevent crashing.
It talks about the difference in run code synchronous and synchronous, and identifies the processing data when it makes sense to run each of them.
Task Module
It will learn how to start tasks and different ways to retrieve results.
The entire chapter will mostly show examples to use the public functions in the Task Module and how to start and retrieve the data after finishing the execution. And it will keep these in a roll and show us how to prevent, or better choice depends on our necessity.
Most cases have a timeout to run the Task and change the time or set it as an infinity. And show the %Task{}
struct, like:
-
Owner - is the PID of the process that started the Task;
-
PID - is the identifier of the Task process itself;
-
Ref - is the process monitor reference.
After the Elixir 1.10 version was implemented, the async_stream
function to create the entire stream pipeline process to not just run asynchronously but laziness, and has some optional parameters, it could be passing, like:
max_concurrency
ordered
on_timeout
Process link
, no_link
, and monitor
:
-
Link - a linked process will be linked to another process; when this process dies, it will die the process related.
-
No Link - a no-linked process when died, die alone, and don’t tell anyone.
-
Monitor - a monitor process will monitor some process; when the process going die, it will notifier the process watching him.
Task.Supervisor
It will monitor/supervise the Task was starting in your application, prevent these tasks from being linked to your application process, and die together with some Task processes.
And explain how to use the Task Supervisor and how to add the Application Supervisor Tree.
It shows the many ways to the specification child process.
How to isolate crashes to specify the strategy to restart the child processes, and has three different values, like:
:temporary
:transient
:permanent