问题描述
我正在研究python线程,并遇到了 join()
.
I was studying the python threading and came across join()
.
作者告诉我们,如果线程处于守护程序模式,那么我需要使用join()
,以便线程可以在主线程终止之前完成自身的工作.
The author told that if thread is in daemon mode then i need to use join()
so that thread can finish itself before main thread terminates.
但是我也看到他使用t.join()
,即使t
不是daemon
but I have also seen him using t.join()
even though t
was not daemon
示例代码是这个
import threading
import time
import logging
logging.basicConfig(level=logging.DEBUG,
format='(%(threadName)-10s) %(message)s',
)
def daemon():
logging.debug('Starting')
time.sleep(2)
logging.debug('Exiting')
d = threading.Thread(name='daemon', target=daemon)
d.setDaemon(True)
def non_daemon():
logging.debug('Starting')
logging.debug('Exiting')
t = threading.Thread(name='non-daemon', target=non_daemon)
d.start()
t.start()
d.join()
t.join()
我不知道t.join()
的用途是什么,因为它不是守护程序,即使删除了它也看不到任何变化
i don't know what is use of t.join()
as it is not daemon and i can see no change even if i remove it
推荐答案
一种有点笨拙的ascii艺术,以演示该机制:join()
大概是由主线程调用的.也可以由另一个线程调用它,但是会不必要地使该图复杂化.
A somewhat clumsy ascii-art to demonstrate the mechanism:The join()
is presumably called by the main-thread. It could also be called by another thread, but would needlessly complicate the diagram.
join
-调用应该放在主线程的轨道上,但是为了表达线程关系并使它尽可能简单,我选择将其放在子线程中.
join
-calling should be placed in the track of the main-thread, but to express thread-relation and keep it as simple as possible, I choose to place it in the child-thread instead.
without join:
+---+---+------------------ main-thread
| |
| +........... child-thread(short)
+.................................. child-thread(long)
with join
+---+---+------------------***********+### main-thread
| | |
| +...........join() | child-thread(short)
+......................join()...... child-thread(long)
with join and daemon thread
+-+--+---+------------------***********+### parent-thread
| | | |
| | +...........join() | child-thread(short)
| +......................join()...... child-thread(long)
+,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, child-thread(long + daemonized)
'-' main-thread/parent-thread/main-program execution
'.' child-thread execution
'#' optional parent-thread execution after join()-blocked parent-thread could
continue
'*' main-thread 'sleeping' in join-method, waiting for child-thread to finish
',' daemonized thread - 'ignores' lifetime of other threads;
terminates when main-programs exits; is normally meant for
join-independent tasks
所以您看不到任何更改的原因是因为您的主线程在join
之后没有执行任何操作.您可能会说join
(仅)与主线程的执行流程相关.
So the reason you don't see any changes is because your main-thread does nothing after your join
.You could say join
is (only) relevant for the execution-flow of the main-thread.
例如,如果您要同时下载一堆页面以将它们串联成一个大页面,则可以使用线程开始并发下载,但是需要等到最后一页/线程完成后才能开始组装一页中的一页.那就是您使用join()
的时间.
If, for example, you want to concurrently download a bunch of pages to concatenate them into a single large page, you may start concurrent downloads using threads, but need to wait until the last page/thread is finished before you start assembling a single page out of many. That's when you use join()
.
这篇关于Python线程中join()的用途是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!