4 ms·
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 th
by smallerfish 3y 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?
- gpderetta 3y agoMy 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.
- ameliaquining 3y agoPython 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-the-global-interpreter-lock 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.
- hot_gril 3y agoThis 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.