Thread.join() 在 python 中究竟做了什么?这是 Thread.join() 的错误用法吗?
What exactly does Thread.join() do in python? Is this incorrect usage of Thread.join()?
我最近开始学习如何在 python 中编写多线程程序,对于初学者,我开始尝试 不使用 使用队列。
在下面的代码中,loadFunction 只是线程的示例目标函数。 应该 等到等待(列表)参数中提供的所有线程都完成执行(我正在尝试使用 join() 实现此目的)。然后之后开始打印出指定的数字范围。
这是期望的行为。
在主程序中,我创建了两个线程,它们不等待任何其他线程开始计数。然后我创建了第三个线程,它应该等待前两个线程执行完才开始计数。
然而,这并没有发生。在测试中,我发现三个线程同时开始执行,而第三个线程并没有像我预期的那样等待前两个线程完成执行。
所以我的问题是,我缺少关于 Thread.join() 函数的哪些知识,我可以对我的代码进行哪些更改以达到预期的结果?
代码:
""" Note: Python 3.3 onwards required since daemon was added as an initializable
property from python 3.3 onwards."""
import threading
def loadFunction(name, start, end, wait=[]):
""" wait should be a list of threads to wait for """
map(lambda th: th.join(), wait)
for number in range(start, end):
print("%s : %d" % (name, number))
if __name__ == "__main__":
t1 = threading.Thread(target=loadFunction, args=("Thread1", 1, 101), name="Thread1" ,daemon=True)
t2 = threading.Thread(target=loadFunction, args=("Thread2", 101, 201), name="Thread2", daemon=True)
t3 = threading.Thread(target=loadFunction, args=("Thread3", 1000, 1101, [t1, t2]), name="Thread3", daemon=True)
t1.start()
t2.start()
t3.start()
# wait for all of the daemon processes to finish before we close the program
t1.join()
t2.join()
t3.join()
print("done!")
部分结果(一运行):
Thread1 : 1
Thread1 : 2
Thread1 : 3
Thread1 : 4
Thread1 : 5
Thread1 : 6
Thread2 : 101
Thread2 : 102
Thread2 : 103
Thread2 : 104
Thread2 : 105
Thread2 : 106
Thread2 : 107
Thread2 : 108
Thread2 : 109
Thread1 : 7
Thread1 : 8
Thread1 : 9
Thread1 : 10
Thread1 : 11
Thread3 : 1000
Thread1 : 12
Thread1 : 13
Thread3 : 1001
Thread1 : 14
Thread3 : 1002
Thread1 : 15
Thread3 : 1003
Thread1 : 16
Thread2 : 110
在编写这段代码时,我想到了两件事(引用自 the official documentation):
join(timeout=None)
Wait until the thread terminates. This blocks the calling
thread until the thread whose join() method is called terminates ...
参考我的示例代码,“调用线程”是调用 loadFunction 函数的那个线程吗?我有一个 slight 怀疑情况并非如此,进程本身调用函数而不是线程,因此线程不会成为等待的人,但过程却在等待。如果是这种情况,我该如何解决?感觉要排队了。。。if首先就是这个原因
和:
A thread can be join()ed many times.
是导致我对同一个线程使用 join 两次的原因。
P.S。我是在 python 中第一次学习线程,但这是在学习 C 中的分叉进程之后,所以也许我 可能 由于这个原因,这里有些混乱。如果两者不相关,那么我很抱歉,我的印象是两者相似(尽管显然不一样,因为一个拆分了进程,而另一个在进程本身内创建了线程)。
谢谢。
如果您想在开始第三个线程之前等待前两个线程完成,请不要在 join()
其他两个线程之前在第三个线程上调用 start()
:
t1.start()
t2.start()
# wait for the threads above to finish before starting the third
t1.join()
t2.join()
t3.start() #now start it
t3.join() # wait for it to finish
would the "calling thread" be whichever thread called the loadFunction function?
调用线程是调用 join
的线程。所以 t1.join()
和 t2.join()
和 t3.join()
导致主线程阻塞,如果 map
没有延迟计算, loadFunction
内部的连接将导致 t3 阻塞.
how would I fix it?
您在 loadFunction
中的连接没有执行,因为 map
在您迭代它之前不会执行任何代码。正如 MaxNoe 建议的那样,您应该改用普通的 for 循环。
def loadFunction(name, start, end, wait=[]):
""" wait should be a list of threads to wait for """
for t in wait:
t.join()
for number in range(start, end):
print("%s : %d" % (name, number))
我最近开始学习如何在 python 中编写多线程程序,对于初学者,我开始尝试 不使用 使用队列。
在下面的代码中,loadFunction 只是线程的示例目标函数。 应该 等到等待(列表)参数中提供的所有线程都完成执行(我正在尝试使用 join() 实现此目的)。然后之后开始打印出指定的数字范围。 这是期望的行为。
在主程序中,我创建了两个线程,它们不等待任何其他线程开始计数。然后我创建了第三个线程,它应该等待前两个线程执行完才开始计数。
然而,这并没有发生。在测试中,我发现三个线程同时开始执行,而第三个线程并没有像我预期的那样等待前两个线程完成执行。
所以我的问题是,我缺少关于 Thread.join() 函数的哪些知识,我可以对我的代码进行哪些更改以达到预期的结果?
代码:
""" Note: Python 3.3 onwards required since daemon was added as an initializable
property from python 3.3 onwards."""
import threading
def loadFunction(name, start, end, wait=[]):
""" wait should be a list of threads to wait for """
map(lambda th: th.join(), wait)
for number in range(start, end):
print("%s : %d" % (name, number))
if __name__ == "__main__":
t1 = threading.Thread(target=loadFunction, args=("Thread1", 1, 101), name="Thread1" ,daemon=True)
t2 = threading.Thread(target=loadFunction, args=("Thread2", 101, 201), name="Thread2", daemon=True)
t3 = threading.Thread(target=loadFunction, args=("Thread3", 1000, 1101, [t1, t2]), name="Thread3", daemon=True)
t1.start()
t2.start()
t3.start()
# wait for all of the daemon processes to finish before we close the program
t1.join()
t2.join()
t3.join()
print("done!")
部分结果(一运行):
Thread1 : 1
Thread1 : 2
Thread1 : 3
Thread1 : 4
Thread1 : 5
Thread1 : 6
Thread2 : 101
Thread2 : 102
Thread2 : 103
Thread2 : 104
Thread2 : 105
Thread2 : 106
Thread2 : 107
Thread2 : 108
Thread2 : 109
Thread1 : 7
Thread1 : 8
Thread1 : 9
Thread1 : 10
Thread1 : 11
Thread3 : 1000
Thread1 : 12
Thread1 : 13
Thread3 : 1001
Thread1 : 14
Thread3 : 1002
Thread1 : 15
Thread3 : 1003
Thread1 : 16
Thread2 : 110
在编写这段代码时,我想到了两件事(引用自 the official documentation):
join(timeout=None)
Wait until the thread terminates. This blocks the calling thread until the thread whose join() method is called terminates ...
参考我的示例代码,“调用线程”是调用 loadFunction 函数的那个线程吗?我有一个 slight 怀疑情况并非如此,进程本身调用函数而不是线程,因此线程不会成为等待的人,但过程却在等待。如果是这种情况,我该如何解决?感觉要排队了。。。if首先就是这个原因
和:
A thread can be join()ed many times.
是导致我对同一个线程使用 join 两次的原因。
P.S。我是在 python 中第一次学习线程,但这是在学习 C 中的分叉进程之后,所以也许我 可能 由于这个原因,这里有些混乱。如果两者不相关,那么我很抱歉,我的印象是两者相似(尽管显然不一样,因为一个拆分了进程,而另一个在进程本身内创建了线程)。
谢谢。
如果您想在开始第三个线程之前等待前两个线程完成,请不要在 join()
其他两个线程之前在第三个线程上调用 start()
:
t1.start()
t2.start()
# wait for the threads above to finish before starting the third
t1.join()
t2.join()
t3.start() #now start it
t3.join() # wait for it to finish
would the "calling thread" be whichever thread called the loadFunction function?
调用线程是调用 join
的线程。所以 t1.join()
和 t2.join()
和 t3.join()
导致主线程阻塞,如果 map
没有延迟计算, loadFunction
内部的连接将导致 t3 阻塞.
how would I fix it?
您在 loadFunction
中的连接没有执行,因为 map
在您迭代它之前不会执行任何代码。正如 MaxNoe 建议的那样,您应该改用普通的 for 循环。
def loadFunction(name, start, end, wait=[]):
""" wait should be a list of threads to wait for """
for t in wait:
t.join()
for number in range(start, end):
print("%s : %d" % (name, number))