Python 多线程不';I don’我没料到会这样
我正在学习Python 3中的线程 这是一个简单的概念示例。我希望每个线程在打印之前等待5秒钟,因此对线程的调用只需1秒,它必须返回到主线程 但是我的代码的输出不是我所期望的 我的代码:Python 多线程不';I don’我没料到会这样,python,multithreading,python-3.x,Python,Multithreading,Python 3.x,我正在学习Python 3中的线程 这是一个简单的概念示例。我希望每个线程在打印之前等待5秒钟,因此对线程的调用只需1秒,它必须返回到主线程 但是我的代码的输出不是我所期望的 我的代码: import threading, time class MyThread(threading.Thread): def __init__(self, num): threading.Thread.__init__(self) self.num = num
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
ing直到线程结束
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
听起来您希望主线程保持每秒打印一次,直到另一个线程完成。但这不会发生,因为主线程在尝试加入一次后将放弃并移动到下一个线程。你应该保持join
ing直到线程结束
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
听起来您希望主线程保持每秒打印一次,直到另一个线程完成。但这不会发生,因为主线程在尝试加入一次后将放弃并移动到下一个线程。你应该保持join
ing直到线程结束
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
听起来您希望主线程保持每秒打印一次,直到另一个线程完成。但这不会发生,因为主线程在尝试加入一次后将放弃并移动到下一个线程。你应该保持join
ing直到线程结束
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
根据您的输出,我认为下面的逻辑必须起作用
根据您的输出,我认为下面的逻辑必须起作用
根据您的输出,我认为下面的逻辑必须起作用
根据您的输出,我认为下面的逻辑必须起作用
在for循环中有1秒的时间延迟。因此,在生成的线程等待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
这指向时间。睡眠(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秒。当你打电话给join的时候,只需要一秒钟。因此,没有线程会在分配的1秒内完成。如果要真正等待生成的线程完成,应执行以下操作:
for i in range(0, 10):
t = MyThread(i)
if t.is_alive():
t.join()
print("I'm the principal thread, too.")
在for循环中有1秒的时间延迟。因此,在生成的线程等待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
这指向时间。睡眠(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秒。当你打电话给join的时候,只需要一秒钟。因此,没有线程会在分配的1秒内完成。如果要真正等待生成的线程完成,应执行以下操作:
for i in range(0, 10):
t = MyThread(i)
if t.is_alive():
t.join()
print("I'm the principal thread, too.")
在for循环中有1秒的时间延迟。因此,在生成的线程等待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
这指向时间。睡眠(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秒。当你打电话给join的时候,只需要一秒钟。因此,没有线程会在分配的1秒内完成。如果要真正等待生成的线程完成,应执行以下操作:
for i in range(0, 10):
t = MyThread(i)
if t.is_alive():
t.join()
print("I'm the principal thread, too.")
在for循环中有1秒的时间延迟。因此,在生成的线程等待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
这指向时间。睡眠(1)
。1号线