python - 模块线程无法按预期工作
问题描述
我正在尝试创建 2 个将同时执行的函数,当我运行代码时,即使我完全导入了线程模块,它也会返回“NameError:name 'Thread' is not defined”。这是我的一些代码:
from threading import *
from time import time, sleep
def one(num):
sleep(num)
def two(num):
sleep(num)
t1 = Thread(target=one, args=(1,))
t2 = Thread(target=two, args=(1,))
x1 = time()
t1.start()
t2.start()
x2 = time()
print(x2-x1)
upd1:我在 linux mint cinnamon 上使用 python 3.9.5
upd2:该代码在 Windows 上运行良好,但在 linux 上却不行
解决方案
推荐阅读
- java - 使用java将文件上传到特定的谷歌驱动器帐户
- multithreading - 为什么 fun 不在 Thread kotlin 中执行?
- vue.js - vuejs 从组件到根的发射方法
- angular - 找不到管道“searchfilter”
- javascript - 难以使用 Flask 托管静态和模板文件
- c# - 在 List 中使用 Class 作为接口
- nginx - EC2 Nginx 反向代理 Angular S3 Bucket 得到 404 Not Found 错误
- java - Json Parser 无法处理请求映射
- maven - 为什么我的测试在 Docker Maven 容器 3.5.4-jdk8 中失败,当它在命令行上使用 maven 通过时
- python - FileNotFoundError at /stores/ - 将本地 json 文件加载到 Django 视图中