How to catch exceptions parent process child process?

A thread is a lightweight entity, supported only by a few of its resources independently. For Python, the thread has its own independent stack, when the thread run error, the thread ends the run, when the need for error handling, the general will be handled in the thread, but only if the main process to handle the exception, then the thread to how can the exception notification to the primary process? For the process, abnormal production of child processes the parent process how to deal with? Package Penalty for Multiprocessing Multiprocessing is a multi-process Python library, API similar threading its implementation of its sub-modules dummy Multiprocessing.process achieved with the same API, the only difference is that, dummy is only one package of threading done it. This I do not know for CPU-intensive or IO intensive program provides convenient, you can switch freely between the multi-process and multi-threaded and simply change one line of code. Call error_callback function in python3 in, Multiprocessing for improved and apply_async (), adds a default parameter error_callback = None, where you can specify the appropriate error handling function, it will appear in the child or the child is abnormal thread running call. corresponding is callback = None parameter (Python2 also supported), it will be called after the child or the child thread running smoothly. the principle is that a child thread _success property or child process, which indicates the run status.




   

[Python]  plain text view  Copy the code
?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
def get( self , timeout = None ):        self .wait(timeout)
         if not self .ready():
             raise TimeoutError
         if self ._success:
             return self ._value
         else :
             raise self ._value
 
     def _set( self , i, obj):
         self ._success, self ._value = obj
         if self ._callback and self ._success:
             self ._callback( self ._value)
         if self ._error_callback and not self ._success:
             self ._error_callback( self ._value)
         self ._event. set ()
         del self ._cache[ self ._job]



Note that, raise self._value statement throws is a Exception.AttributeError, it can be seen only throw two errors, one is out error, one is AttributeError. And we can use is AttributeError, in thread or process execution kinds of errors, we only need to use the appropriate error message format string to pass out through AttributeError, in error_callback to get this error message during further processing can be, if you only need to inform the host process wrong, then you only need to raise AttributeError. check the thread exits the state due to the abnormal thread to exit and not the same as when exitcode when the normal exit, and after the end of the thread to run, but the memory will not be recovered immediately, based on this, we can by examining exitcode way to trap errors that appear in the thread, this method from PengMeng's Blog, generally, Python property in the process, may be able to achieve the same effect and exitcode here is no longer in-depth discussion. for more technical information may concern : gzitcast



Guess you like

Origin www.cnblogs.com/heimaguangzhou/p/11464960.html