25/28
Part 6 of 6 · What interviewers ask nextLearn
Threads, asyncio or processes
The last follow-up asks why threads, and not something else.
| Threads | asyncio | Processes | |
|---|---|---|---|
| Best at | waiting, with ordinary blocking libraries | waiting, on thousands of connections | Python computing, like parsing |
| Each one costs | an OS thread | a task object in one thread | a whole process |
| Shared state | needs a lock once workers share it | can only switch at an await | not shared, data is pickled across |
| Watch for | the OS thread limit | one blocking call stalls every task | startup and pickling cost |
Three tools for the same problem. A crawler is I/O-bound: its time goes to waiting, so it's threads or asyncio.
asyncio wins when you want thousands of connections open at once, and every library you call is async, like an httpx or aiohttp client. Threads win when your fetch is ordinary blocking code, like urllib or requests, and a few dozen workers are enough.
Parsing pages in Python becomes the bottleneck. What do you add?