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.

ThreadsasyncioProcesses
Best atwaiting, with ordinary blocking librarieswaiting, on thousands of connectionsPython computing, like parsing
Each one costsan OS threada task object in one threada whole process
Shared stateneeds a lock once workers share itcan only switch at an awaitnot shared, data is pickled across
Watch forthe OS thread limitone blocking call stalls every taskstartup 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.

Predict

Parsing pages in Python becomes the bottleneck. What do you add?