Puzzle 24: Explanation
Let’s find out how threads and processes work in Python.
We'll cover the following...
We'll cover the following...
Try it yourself
Try executing the code below to verify the results:
Press + to interact
Python 3.8
from concurrent.futures import ProcessPoolExecutorfrom itertools import repeatguilty = 0def juror():global guiltyguilty += 1with ProcessPoolExecutor() as pool:for _ in repeat(None, 12):pool.submit(juror)print(guilty)
Explanation
Both threads and processes are concurrent units of work. The main difference is that threads share the same memory space and processes don’t.
This means that if we have a global variable (like guilty
...