Chapter IV
Do multiple things at the same time
OBJECTIVES
Learn and use Threads
Learn and use Processes
Use parallel execution
Use concurrency
Introduction
A couple of definitions are required so that we have the appropriate vocabulary and we establish a common ground for our conversation.
Term
Definition
Process
A coherent unit of code that is executed as a hole. Usually by a single instance of a Python interpreter.
Thread
A piece of code, usually a single function, that is executed as part of a process or task. Multiple threads may be executed in parallel.
Concurrency
Executing threads and/or tasks in parallel, they may compete for the same resources and deadlock, livelock, starvation may occur
Parallelism
Executing independent threads and/or tasks in parallel. Synchronisation may be needed for starting the threads or collecting the results.
Spawn
When a Process creates another Process almost identical to the original one.
Fork
When a Process creates an identical copy of itself.
Threads
When we want to execute several pieces of code on our processor we may use threads. Threads are a convenient way to separate a function or a couple of functions so that they can be executed in parallel. We can multiply the number of instances of the same code.
Processes
Parallelism
Concurrency
Interprocess Communication
Now that we have acquired the knowledge to create multiple processes and thread we should be able to use them wisely to achieve our goals. An important part of building solutions using multiprocessing and multithreading is to synchronize those entities via communication.
The solution
Last updated
Was this helpful?