我正在学习Python 3中的线程。
这是一个简单的概念示例。我希望每个线程在打印之前等待5秒,因此对线程的调用只有1秒大,它必须返回主线程。
但我的代码输出并不是我所期望的。
我的代码:
import threading, time
class MyThread(threading.Thread):
def __init__(self, num):
threading.Thread.__init__(self)
self.num = num
def run(self):
time.sleep(5)
print ("I'm thread number: ", self.num)
print ("I'm the principal thread.")
for i in range(0, 10):
t = MyThread(i)
t.start()
t.join(1)
print("I'm the principal thread, too.")
输出:
I'm the principal thread.
I'm the principal thread, too.
I'm the principal thread, too.
I'm the principal thread, too.
I'm the principal thread, too.
I'm thread number: 0
I'm the principal thread, too.
I'm thread number: 1
I'm the principal thread, too.
I'm thread number: 2
I'm the principal thread, too.
I'm thread number: 3
I'm the principal thread, too.
I'm thread number: 4
I'm the principal thread, too.
I'm thread number: 5
I'm the principal thread, too.
I'm thread number: 6
I'm thread number: 7
I'm thread number: 8
I'm thread number: 9
预期的输出是这样的:
I'm the principal thread.
I'm the principal thread, too.
I'm the principal thread, too.
I'm the principal thread, too.
I'm the principal thread, too.
I'm thread number: 0
I'm the principal thread, too.
I'm the principal thread, too.
I'm the principal thread, too.
I'm the principal thread, too.
I'm thread number: 1
I'm the principal thread, too.
I'm the principal thread, too.
I'm the principal thread, too.
I'm the principal thread, too.
I'm thread number: 2
I'm the principal thread, too.
...
我做错了什么?
答案 0 :(得分:1)
听起来你希望主线程每秒继续打印一次,直到另一个线程完成。但这不会发生,因为在尝试join
一次后,主线程将放弃并移动到下一个线程。你应该保持join
直到线程结束。
import threading, time
class MyThread(threading.Thread):
def __init__(self, num):
threading.Thread.__init__(self)
self.num = num
def run(self):
time.sleep(5)
print ("I'm thread number: ", self.num)
print ("I'm the principal thread.")
for i in range(0, 10):
t = MyThread(i)
t.start()
while True:
t.join(1)
print("I'm the principal thread, too.")
if not t.isAlive(): break
答案 1 :(得分:1)
根据您的输出,我认为以下逻辑必须有效
import threading, time
class MyThread(threading.Thread):
def __init__(self, num):
threading.Thread.__init__(self)
self.num = num
def run(self):
time.sleep(5)
print ("I'm thread number: ", self.num)
print ("I'm the principal thread.")
for i in range(0, 10):
t = MyThread(i)
t.start()
t.join(1)
## You join Times out here, since the thread didnt finish in 1 sec
##(its sleeping for 5 secs)
while t.isAlive():
time.sleep(1)
print("I'm the principal thread, too.")
答案 2 :(得分:1)
for循环中有一秒的时滞。因此,在生成的线程等待5秒之前,“我也是主线程。”被打印4次。到那时,num = 0的线程已经完成等待5秒。所以它打印出来。
for i in range(0, 10):
t = MyThread(i)
t.start()
t.join(1)
print("I'm the principal thread, too.")
在此块中,请注意t.join(1)
。它等待1秒。线程't'完成,但因为线程实际上等待5秒。在开始之后,它继续循环。因此,
I'm thread number: 0
I'm the principal thread, too.
I'm thread number: 1
这指向time.sleep(1)
。线程编号1在1秒后产生。产生了第0个线程,等等。
循环只运行10次,因此“我也是主线程”。只打印10次。
I'm the principal thread.
I'm the principal thread, too.
I'm the principal thread, too.
I'm the principal thread, too.
I'm the principal thread, too.
I'm thread number: 0
I'm the principal thread, too.
I'm thread number: 1
I'm the principal thread, too.
I'm thread number: 2
I'm the principal thread, too.
I'm thread number: 3
I'm the principal thread, too.
I'm thread number: 4
I'm the principal thread, too.
I'm thread number: 5
I'm the principal thread, too.
此时,循环结束,但剩余的线程在等待5秒后仍在等待打印出来,因此您看到了:
I'm thread number: 6
I'm thread number: 7
I'm thread number: 8
I'm thread number: 9
输出中的差异问题是每个生成的线程等待5秒。你打电话加入它只有一秒钟。因此,没有线程将在分配的1秒内完成。如果你想真正等待生成的线程完成,你应该这样做:
for i in range(0, 10):
t = MyThread(i)
if t.is_alive():
t.join()
print("I'm the principal thread, too.")