I came across this interesting statement in "Caveats" section of the documentation for the thread
module today:
Not all built-in functions that may block waiting for I/O allow other threads to run. (The most popular ones (
time.sleep()
,file.read()
,select.select()
) work as expected.)
Pretty much everywhere else I've ever seen Python threads discussed, there has always been an assumption that all built-in functions which do I/O will release the GIL, meaning other threads can run while the function blocks. As far as I knew, the only risk of an I/O operation blocking other threads would be if it was being made to a buggy C-extension that neglected to release the GIL.
So, is this statement from the thread
docs actually true? Are there any built-in, blocking I/O operations that don't release the GIL? I haven't been able to find any specific examples thus far.
Blocking I/O It means when the thread invoke a write() or read(), then the thread is blocked until there is some data available for read, or the data is fully written.
The condition occurs when one thread tries to modify a shared resource at the same time that another thread is modifying that resource – this leads to garbled output, which is why threads need to be synchronized. The threading module of Python includes locks as a synchronization tool.
A function is blocking if it has to wait for something to complete. Yes, every function is blocking — no matter if you are doing I/O or doing CPU task. Everything takes some time. If a function is doing some task which is making the CPU work, then it is blocking the function from returning.
Each CPU core can have up to two threads if your CPU has multi/hyper-threading enabled. You can search for your own CPU processor to find out more. For Mac users, you can find out from About > System Report. This means that my 6-Core i7 processor has 6 cores and can have up to 12 threads.
Here's the official word from Guido van Rossum on this one:
Not in my wildest dreams could I have expected that that claim would still be in the docs 20 years later. :-) Please get rid of it.
So the answer to my question is "No".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With