首页 > 解决方案 > 模块线程无法按预期工作

问题描述

我正在尝试创建 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 上却不行

标签: pythonmultithreading

解决方案


推荐阅读