我正在研究一个小程序来练习多线程,其中我创建了两个计时器线程来输出当前时间。我已经做了两个定时器功能来输出当前时间,我都想等待一段时间,然后再输出一次。我试图使用time.sleep来执行此操作,但是在运行代码时,出现一条错误消息:AttributeError:'time.struct_time'对象没有属性'sleep'。为什么会出现此错误?我用过time.sleep之前没有任何问题。谢谢!
import time
import threading
#timer function that outputs current time
def timerOne(time, number):
for n in range(number):
time.sleep(0.2)
print(time.asctime(time))
def timerTwo(time, number):
for n in range(number):
time.sleep(0.2)
print(time.asctime(time))
t = time.localtime()
numberOfIterations = 3
t1 = threading.Thread(target=timerOne, args=(t, numberOfIterations))
t2 = threading.Thread(target=timerTwo, args=(t, numberOfIterations))
t1.start()
t2.start()
t1.join()
t2.join()