Summary - Multiprocessing
- Python course
- Expert
- Lesson 30/47
- 5 min
Combine Process, Pool and Queue to write a parallel worker.
Recap: Multiprocessing
In this group you learned process-based parallelism. You started new processes with Process and managed their lifecycle with start and join.
You passed data between processes with Queue. You did parallel mapping with Pool. You used a shared list with Manager. Now you will combine these into a parallel worker: a Process, a Queue and a computation together.
Practice Write a parallel worker
Import with from multiprocessing import Process, Queue. Define a worker(q, val) function that does q.put(val * 2). Create a queue with q = Queue(). Create a process with Process(target=worker, args=(q, 21)), call .start() and .join(). Print q.get().
Starter code
# write the worker function and queue
# start a process and get the resultSample solution
from multiprocessing import Process, Queue
def worker(q, val):
q.put(val * 2)
q = Queue()
p = Process(target=worker, args=(q, 21))
p.start()
p.join()
print(q.get())Expected output
42
What you learned
In this group you learned process-based parallelism in Python. You saw new processes with Process and lifecycle management with start and join.
You used Queue for inter-process data, Pool for parallel mapping and Manager for shared objects. In the summary you combined these into a parallel worker. This knowledge is critical for speeding up CPU-bound work.
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which tool for which job?
- Process for parallel mapping
- Queue for inter-process data Correct answer
- Manager for function calls
- Pool to start a single process
Explanation: answer: Queue for inter-process data. Pool does parallel mapping, Process starts a single process.
2 Match all multiprocessing concepts
| Term | Meaning |
|---|---|
Process |
New process |
Queue |
Data queue |
Pool |
Process pool |
Manager |
Shared object |
3 Final blank
from multiprocessing import Process, ____
q = Queue()
from multiprocessing import Process, Queue
q = Queue()
Explanation: Queue is the inter-process queue.