Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there any built-in functions which block on I/O that don't allow other threads to run?

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.

like image 784
dano Avatar asked Jul 18 '14 18:07

dano


People also ask

Why do threads block on I O?

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.

What is threading lock Python?

The condition occurs when one thread tries to modify a shared resource at the same time that another thread is modifying that resource – t​his leads to garbled output, which is why threads need to be synchronized. The threading module of Python includes locks as a synchronization tool.

What is a blocking function Python?

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.

How many Python threads can I run?

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.


1 Answers

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".

like image 55
2 revs Avatar answered Sep 21 '22 06:09

2 revs