【发布时间】:2010-07-15 18:20:17
【问题描述】:
使用本网站的代码:http://www.saltycrane.com/blog/2008/09/simplistic-python-thread-example/
代码是
import time
from threading import Thread
def myfunc(i):
print "sleeping 5 sec from thread %d" % i
time.sleep(5)
print "finished sleeping from thread %d" % i
for i in range(10):
t = Thread(target=myfunc, args=(i,))
t.start()
我得到这个输出:
sleeping 5 sec from thread 0
sleeping 5 sec from thread 1
sleeping 5 sec from thread 2
sleeping 5 sec from thread 3
sleeping 5 sec from thread 4
sleeping 5 sec from thread 5
sleeping 5 sec from thread 6
sleeping 5 sec from thread 7
sleeping 5 sec from thread 8
sleeping 5 sec from thread 9
finished sleeping from thread 0
finished sleeping from thread 2
finished sleeping from thread 4
finished sleeping from thread 1finished sleeping from thread 6finished sleeping from thread 8
finished sleeping from thread 5finished sleeping from thread 7finished sleeping from thread 9
finished sleeping from thread 3
这里发生了什么?我可以接受没有按顺序打印的线程,因为这是可能的,但为什么它们最后没有在换行符上打印?我在windows xp下使用python 2.6
【问题讨论】:
标签: python multithreading