Yashotantra Academy logo
← Python theorytheory 0/50 · 0%
level 1
Curious · 0 XP
streak
0d
perfect
0
badges
0

100 XP to Apprentice

Async · hard

38. Concurrency: threading vs multiprocessing

The GIL, threads for I/O-bound work, processes for CPU-bound work.

1 min read3 questionsup to 50 XPtopic 38/50

Read in another language

AI translates the explanation and quiz text while preserving code and answer scoring.

CPython's Global Interpreter Lock (GIL) allows only one thread to execute Python bytecode at a time, so `threading` helps for I/O-bound work (waiting on network/disk) but not CPU-bound work.

python
import threading

def fetch(url, results, i):
    results[i] = f"fetched {url}"

results = [None, None]
threads = [threading.Thread(target=fetch, args=(f"url{i}", results, i)) for i in range(2)]
for t in threads: t.start()
for t in threads: t.join()

For CPU-bound work (heavy computation like hashing millions of items), `multiprocessing` spawns separate processes, each with its own interpreter and GIL, achieving real parallelism at the cost of inter-process communication overhead.

How to study this topic

  1. 1. Read the explanation once end to end, skipping the code.
  2. 2. Re-read the code snippets and say out loud what each line does.
  3. 3. Answer the quiz without scrolling back — that is the real check.
  4. 4. Read every explanation, even for the answers you got right.

Scoring

  • · Up to 30 XP from quiz accuracy.
  • · +15 XP for a perfect first attempt.
  • · +5 XP while a 3-day streak is running.
  • · 75% or higher marks the topic complete.

Check your understanding

  1. 1. What does the GIL prevent?

  2. 2. When is `threading` most useful despite the GIL?

  3. 3. How does `multiprocessing` achieve true parallelism?

Badges

First light
Pass your first topic
Sharp shooter
3 perfect first-try quizzes
On a roll
Study 3 days in a row
Week warrior
Study 7 days in a row
Grinder
Earn 500 XP
Deep diver
Earn 1500 XP