python - Unclear behavior of multithreading application: can't exit thread -
multithreading app on python 2.7. use "threading" , "thread" libraries.
main thread had started other 10 threads, work. have 1 shared class data (singletone). don't use thread blocking, , it's seems good. main thread starting threads "start" , "join" methods of threading class. 1 of ten threads, starting every 10 seconds , math calculation. when work complete, thread invoke "thread.exit()".
and main thread did not have result of 1 thread. thread end! , strings of code complete, main thread stops on "join" instruction , did not response.
p.s. i'm not native english speacker, , discribe problem difficult. please tolerant.
code example:
while true: all_result = check_is_all_results() time.sleep(1) if (all_result): print app_data.task_table app_data.flag_of_close = true time.sleep(2) # Задержка на всякий случай if (app_data.flag_of_close): terminate() print u"test" if len(app_data.ip_table[app_data.cfg.my_ip]['tasks']): if (app_data.cfg.multithreading or app_data.complete_task.is_set()): job = worker(app_data, srv.taskresultsendtoslaves, app_data.ip_table[app_data.cfg.my_ip]['tasks'].pop()) job.setdaemon(true) job.start() ########################################################### class worker(threading.thread): def __init__(self, data, sender, taskname): self.data = data self.sender = sender self.taskname = taskname threading.thread.__init__(self) def run(self): import thread self.data.complete_task.clear() tick_before = time.time() startupinfo = subprocess.startupinfo() startupinfo.dwflags |= subprocess.startf_useshowwindow startupinfo.wshowwindow = subprocess.sw_hide p = subprocess.popen(self.data.cfg.path_interpreter + " " + self.data.cfg.path_tasks + self.taskname, startupinfo=startupinfo, shell=false, stdout=subprocess.pipe) job_result, err = p.communicate() tick_after = time.time() work_time = tick_after - tick_before self.data.task_table[self.taskname]['status'] = 'complete' self.data.task_table[self.taskname]['result'] = job_result self.data.task_table[self.taskname]['time'] = work_time tr = threading.thread(target=self.sender, name="sender", args=(self.taskname, )) tr.setdaemon(true) tr.start() tr.join() self.data.complete_task.set() thread.exit()
sometimes main infinite loop, calls worker, not print "test", , not response.
your worker threads spawning subprocesses. unfortunately, never works right, because first done fork copies executing thread parent process. sorry, program not reliable until restructure it. here background information links more information:
https://stackoverflow.com/a/32107436/3577601
Comments
Post a Comment