python-3.x - time.sleep 似乎阻塞了其他线程
问题描述
我有一个简单的 Python 3 程序,其中一个子线程处于休眠状态,而主线程似乎也被阻塞了。为什么不sleep
将cpu切换到主线程?
import threading
import time
def hello():
print('hello')
while True:
time.sleep(10000)
threading.Thread(hello()).start()
print('world')
输出:
hello
这个词world
从未印刷过。
解决方案
您正在hello
通过使用 this 调用主线程,hello()
这是一个函数调用(因为()
),而不是函数引用。参考(名称)只是hello
.
也许尝试:
threading.Thread(target=hello).start()
推荐阅读
- python - 基本 Flask 应用程序未在 Heroku Postgres 数据库中创建表
- deployment - 如何在远程服务器上部署网站,以便局域网中的其他人只能使用 IP 地址和端口号访问它?
- laravel - 如何使用按钮清除数据?拉拉维尔
- asp.net - 如何在中继器控件中显示响应式图片库?
- python - Tkinter - 同时使用滑块旋转图像
- javascript - 将视频 blob/块转换为 MP4 或其他视频容器
- reactjs - ReactJS Mobx 项目没有更新
- python - 一起使用 while 循环和 try-except
- c# - 将记录添加到多个 SQL 表的 ASP.NET C# 循环意外停止而没有错误
- c# - Wix 工具集引导程序显示选项以选择/选择要安装的 msi 包