python 執行緒中join方法的使用

2021-06-20 22:33:23 字數 3245 閱讀 5194

1、作用:讓子執行緒插入主線程,可理解為在在join呼叫處把子執行緒的code move到了主線程,直到被插入的code執行結束,主線程接著下面的繼續**執行

2、觸發條件:手動呼叫或主線程要退出時自動呼叫

3、引數說明:join方法可可傳入引數表示子執行緒加入主線程的執行時間,超出此時間,強制結束子執行緒,不傳引數表示時間不限!

4、example

a) 主線程**將要結束時自動呼叫       

from threading import thread

import threading

from time import sleep

import signal

import traceback

import sys

import os

class datagenerator(thread):

def __init__(self, topic_dir):

self.topic_dir = topic_dir

self.stopflag = false

threading.thread.__init__(self, name="thread_generator_")

def run(self):

self.stoped = false

while not self.stopflag:

print "test_data"

shell_cmd = "sh /usr/local/data_generator/do.sh " + self.topic_dir

# os.system(shell_cmd)

print "runing.....\n"

sleep(1)

self.stoped = true

def join(self, timeout=none):

self.stopflag = true

while not self.stoped:

print "waiting for stopped\n"

sleep(1)

threading.thread.join(self, timeout)

if __name__ == "__main__":

if len(sys.argv) < 2:

print "please set the topic_dirs \n"

os._exit(-1)

stop_flag = false

generators =

for topic_dir in sys.argv[1:]:

generator = datagenerator(topic_dir)

for g in generators:

g.start()

print "start finished!\n"

# while not stop_flag:

# sleep(1)

# sleep(2)

b) 主線程結束前,手動呼叫join(通過訊號量呼叫)

from threading import thread

import threading

from time import sleep

import signal

import traceback

import sys

import os

class datagenerator(thread):

def __init__(self, topic_dir):

self.topic_dir = topic_dir

self.stopflag = false

threading.thread.__init__(self, name="thread_generator_")

def run(self):

self.stoped = false

while not self.stopflag:

print "test_data"

shell_cmd = "sh /usr/local/data_generator/do.sh " + self.topic_dir

# os.system(shell_cmd)

print "runing.....\n"

sleep(1)

self.stoped = true

def join(self, timeout=none):

self.stopflag = true

while not self.stoped:

print "waiting for stopped\n"

sleep(1)

threading.thread.join(self, timeout)

if __name__ == "__main__":

if len(sys.argv) < 2:

print "please set the topic_dirs \n"

os._exit(-1)

stop_flag = false

generators =

for topic_dir in sys.argv[1:]:

generator = datagenerator(topic_dir)

def stop(n=0, e=0):

global stop_flag

for g in generators:

#logger.error("stopping " + str(t))

try:

g.join(60)

except:

print traceback.format_exc()

#logger.error(traceback.format_exc())

stop_flag = true

#ctrl+c

signal.signal(signal.sigterm, stop)

#kill

signal.signal(signal.sigint, stop)

for g in generators:

g.start()

print "start finished!\n"

while not stop_flag:

sleep(1)

# sleep(2)

print "stop finished!\n"

5 、總結:以上**可在win linux上正常執行,over!

執行緒中的join 方法

作用 join 方法的作用就是讓主線程等待子執行緒執行結束之後再執行主線程。public class father catch interruptedexception e static class son extends thread override public void run system...

執行緒中的join 方法

作用 join 方法的作用就是讓主線程等待子執行緒執行結束之後再執行主線程。public class father catch interruptedexception e static class son extends thread override public void run system...

多執行緒中的join 方法

直接看 package net.csdn.demo01 public class mythread extends thread package net.csdn.demo01 public class testmythread 執行結果 張三 1張三 2張三 3張三 4張三 5張三 6張三 7張三...