Hacker News new | ask | show | jobs
by smallerfish 1008 days ago
Maybe I missed it, but how do the threads circumvent the GIL?

> When a request is waiting on the network, another thread is executing.

I'm guessing this is the meat, but what controls that? What other operations allow the GIL to switch to another thread?

2 comments

Python functions implemented in C can release the GIL when they're doing something that doesn't directly involve manipulating Python objects, and then re-acquire it when they're done: https://docs.python.org/3/c-api/init.html#thread-state-and-t...

All I/O functions in the standard library do this when blocked.

This is a far better explanation than the usual opaque "it's concurrent but not parallel" that I'd argue isn't even correct (cause two C calls on separate threads are running in parallel if they don't hold the GIL). Or "it's multithreading but not multiprocessing" which misses the point.
My understanding is that the GIL is typically released around blocking operations. Aside for allowing actual concurrency for I/O heavy programs, it would be a trivial way to deadlock if it wasn't.