python线程join,Python线程中的join()有什么用?

I was studying the python threading and came across 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.

but I have also seen him using t.join() even though t was not daemon

example code is this

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()

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

解决方案

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 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

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.

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().

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值