Python线程中join()有什么用? [英] What is the use of join() in Python threading?

查看:24
本文介绍了Python线程中join()有什么用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在研究 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-art 来演示机制: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-calling应该放在主线程的轨道上,但是为了表达线程关系,尽量简单,我选择放在子线程中.

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()有什么用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆