我有一个简单的 Python 3 程序,其中子线程处于休眠状态,并且主线程似乎也被阻塞。为什么sleep
不将CPU切换到主线程?
import threading
import time
def hello():
print('hello')
while True:
time.sleep(10000)
threading.Thread(hello()).start()
print('world')
输出:
hello
“世界”这个词从未被打印出来。
最佳答案
您正在使用 hello()
在主线程上调用 hello
,这是一个函数调用(因为 ()
),不是函数引用。引用(名称)只是 hello
。
也许可以尝试一下:
threading.Thread(target=hello).start()
关于python-3.x - time.sleep 似乎阻塞了其他线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57567447/