[英]Multithreading doesn't work like I expect it to
我正在学习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.
...
我究竟做错了什么?
听起来您希望主线程每秒继续打印一次,直到另一线程完成。 但这不会发生,因为在尝试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
根据您的输出,我认为以下逻辑必须有效
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.")
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.")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.