Search⌘ K
AI Features

Puzzle 24: Explanation

Explore the key differences between Python threads and processes, including memory sharing, thread safety, and execution contexts. Understand when to apply threads for I/O-bound tasks and processes for CPU-bound workloads, and recognize the challenges in parallel programming.

We'll cover the following...

Try it yourself

Try executing the code below to verify the results:

Python 3.8
from concurrent.futures import ProcessPoolExecutor
from itertools import repeat
guilty = 0
def juror():
global guilty
guilty += 1
with 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 ...