我在研究python线程的时候偶然发现了join()
。
作者告诉我,如果线程处于守护进程模式,那么我需要使用join()
,这样线程就可以在主线程终止之前完成自己的工作。
但我也见过他使用t.join()
,尽管t
不是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()
的用途,因为它不是守护进程,即使我删除它也看不到任何更改
发布于 2013-02-26 18:00:21
演示该机制的一个有点笨拙的ascii艺术:join()
大概是由主线程调用的。它也可以由另一个线程调用,但会不必要地使图表复杂化。
join
-calling应该放在主线程的轨道中,但为了表达线程关系并使其尽可能简单,我选择将其放在子线程中。
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
(仅)与主线程的执行流相关。
例如,如果您想并发下载一组页面以将它们连接成一个大页面,您可以使用线程开始并发下载,但需要等到最后一个页面/线程完成后才能开始组装多个页面中的单个页面。这就是你使用join()
的时候。
发布于 2019-02-26 01:50:04
在python3.x中,join()用于连接线程和主线程,也就是说,当join()用于特定线程时,主线程将停止执行,直到连接的线程执行完毕。
#1 - Without Join():
import threading
import time
def loiter():
print('You are loitering!')
time.sleep(5)
print('You are not loitering anymore!')
t1 = threading.Thread(target = loiter)
t1.start()
print('Hey, I do not want to loiter!')
'''
Output without join()-->
You are loitering!
Hey, I do not want to loiter!
You are not loitering anymore! #After 5 seconds --> This statement will be printed
'''
#2 - With Join():
import threading
import time
def loiter():
print('You are loitering!')
time.sleep(5)
print('You are not loitering anymore!')
t1 = threading.Thread(target = loiter)
t1.start()
t1.join()
print('Hey, I do not want to loiter!')
'''
Output with join() -->
You are loitering!
You are not loitering anymore! #After 5 seconds --> This statement will be printed
Hey, I do not want to loiter!
'''
发布于 2019-01-22 17:41:51
此示例演示.join()
操作:
import threading
import time
def threaded_worker():
for r in range(10):
print('Other: ', r)
time.sleep(2)
thread_ = threading.Timer(1, threaded_worker)
thread_.daemon = True # If the main thread is killed, this thread will be killed as well.
thread_.start()
flag = True
for i in range(10):
print('Main: ', i)
time.sleep(2)
if flag and i > 4:
print(
'''
Threaded_worker() joined to the main thread.
Now we have a sequential behavior instead of concurrency.
''')
thread_.join()
flag = False
输出:
Main: 0
Other: 0
Main: 1
Other: 1
Main: 2
Other: 2
Main: 3
Other: 3
Main: 4
Other: 4
Main: 5
Other: 5
Threaded_worker() joined to the main thread.
Now we have a sequential behavior instead of concurrency.
Other: 6
Other: 7
Other: 8
Other: 9
Main: 6
Main: 7
Main: 8
Main: 9
https://stackoverflow.com/questions/15085348
复制相似问题