我在 thread
文档的“注意事项”部分看到了这个有趣的声明。今天的模块:
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.)
在我见过的几乎所有讨论 Python 线程的地方,总是假设所有执行 I/O 的内置函数都会释放 GIL,这意味着其他线程可以在函数阻塞时运行。据我所知,I/O 操作阻塞其他线程的唯一风险是,如果它是针对忽略释放 GIL 的错误 C 扩展进行的。
那么,thread
文档中的这个声明真的是真的吗?是否有任何不释放 GIL 的内置阻塞 I/O 操作?到目前为止,我还没有找到任何具体的例子。
最佳答案
这是 official word from Guido van Rossum关于这个:
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.
所以我的问题的答案是“否”。
关于python - 是否有任何内置函数会阻塞 I/O 而不允许其他线程运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24831458/