Methods python capture the main thread of the child thread

This article describes how to find out more python capture the main thread child thread, with some reference value, and interested friends can reference
recently encountered a problem when doing a project, the main thread can not be captured son thread thrown.

Look at the definition of a class thread

''''' 
Created on Oct 27, 2015 
  
@author: wujz 
'''
import threading 
  
class runScriptThread(threading.Thread): 
 def __init__(self, funcName, *args): 
  threading.Thread.__init__(self) 
  self.args = args 
  self.funcName = funcName 
   
 def run(self): 
  try: 
   self.funcName(*(self.args)) 
  except Exception as e: 
   raise e

Very simple, pass the method to call, and enable a new thread to run this method.

In the main thread, the thread object is when you start a class, it should declare an object and then restart it, the following example

import runScriptThread,traceback 
  
if __name__=='__main__': 
 sth = 'hello world'
 try: 
  aChildThread = runScriptThread(printSth, sth) 
  aChildThread.start() 
  aChildThread.join() 
 except Exception as e: 
  print(str(traceback.format_exc()))

But this code, the main method can not catch the exception sub-thread, because the start () method will open up a new sub-thread stack, the stack can not be the main method to capture the exception.
The solution is simple, that is by setting a member variable thread flag is abnormal exit, when the thread exits abnormally, it made a mark. Then checks the value of change in the main thread of the threaded end of the run flag, if abnormal, the abnormality information and then back through the sys and the traceback, and can be thrown. The rewritten exception classes:

''''' 
Created on Oct 27, 2015 
  
@author: wujz 
'''
import threading,traceback,sys 
  
class runScriptThread(threading.Thread): #The timer class is derived from the class threading.Thread 
 def __init__(self, funcName, *args): 
  threading.Thread.__init__(self) 
  self.args = args 
  self.funcName = funcName 
  self.exitcode = 0
  self.exception = None
  self.exc_traceback = '' 
   
 def run(self): #Overwrite run() method, put what you want the thread do here 
  try: 
   self._run() 
  except Exception as e: 
   self.exitcode = 1  # 如果线程异常退出,将该标志位设置为1,正常退出为0 
   self.exception = e 
   self.exc_traceback = ''.join(traceback.format_exception(*sys.exc_info())) #在改成员变量中记录异常信息 
   
 def _run(self): 
  try: 
   self.funcName(*(self.args)) 
  except Exception as e: 
   raise e 

After rewriting the main thread:

import runScriptThread,traceback 
  
if __name__=='__main__': 
 sth = 'hello world'
 try: 
  aChildThread = runScriptThread(printSth, sth) 
  aChildThread.start() 
  aChildThread.join() 
 except Exception as e: 
  print(aChildThread.exc_traceback)

We recommend learning Python buckle qun: 774711191, look at how seniors are learning! From basic web development python script to, reptiles, django, data mining, etc. [PDF, actual source code], zero-based projects to combat data are finishing. Given to every little python partner! Every day, Daniel explain the timing Python technology, to share some learning methods and the need to pay attention to small details, click on Join us python learner gathering
all of the above is the entire contents of this article, we want to help learning

Published 35 original articles · won praise 9 · views 10000 +

Guess you like

Origin blog.csdn.net/haoxun02/article/details/104254674
Recommended