from threading import *
from time import *
class MyThread(Thread):
def __init__(self,x):
self.x = x
Thread.__init__(self)
def run(self):
sleep(2)
print(self.x)
if __name__=='__main__':
threads = []
for i in range(5):
threads.append(MyThread('Hello'))
for i in range(5):
threads[i].start()
for i in range(5):
threads[i].join()
此代码打印'Hello' 10次,但如果我发表评论“sleep(2)”,则会打印“Hello” 5次。
sleep()函数有什么问题?或问题在哪里?
我使用的是Python3000。
答案 0 :(得分:2)
看起来你遇到了Python bug跟踪器issue 6750中记录的问题。检查问题的修复程序将出现在Python 3.1的下一个维护版本中,如果有的话,或者在Python 3.2中。
$ python3.1 test_thread.py
Hello
Hello
Hello
Hello
Hello
Hello
Hello
$ python3.2 test_thread.py
Hello
Hello
Hello
Hello
Hello
答案 1 :(得分:0)
我认为你在Python 3中发现了一个错误 - 我可以在最新发布的Py 3.1下重现你的结果,但是2.6结果是预期的结果(只有五行发出,尽管新行可以获得令人惊讶的是,这些内容符合线程行为的规范。请在bugs.python.org !!!
打开一个错误