📜  在Python中处理调用者线程中的线程异常

📅  最后修改于: 2022-05-13 01:54:40.146000             🧑  作者: Mango

在Python中处理调用者线程中的线程异常

Python中的多线程可以通过使用threading库来实现。为了调用线程,调用者线程创建一个线程对象并在其上调用 start 方法。一旦调用了 join 方法,就会启动它的执行并执行类对象的 run 方法。

对于异常处理,使用 try-except 块来捕获在 try 块中引发的异常,并在 except 块中进行相应处理

例子:

Python3
# Importing the modules
import threading
import sys
 
# Custom Thread Class
class MyThread(threading.Thread):
    def someFunction(self):
        print("Hello World")
    def run(self):
          self.someFunction()
    def join(self):
        threading.Thread.join(self)
 
# Driver function
def main():
    t = MyThread()
    t.start()
    t.join()
 
# Driver code
if __name__ == '__main__':
    main()


Python3
# Importing the modules
import threading
import sys
 
# Custom Exception Class
class MyException(Exception):
    pass
 
# Custom Thread Class
class MyThread(threading.Thread):
     
  # Function that raises the custom exception
    def someFunction(self):
        name = threading.current_thread().name
        raise MyException("An error in thread "+ name)
 
    def run(self):
       
        # Variable that stores the exception, if raised by someFunction
        self.exc = None           
        try:
            self.someFunction()
        except BaseException as e:
            self.exc = e
       
    def join(self):
        threading.Thread.join(self)
        # Since join() returns in caller thread
        # we re-raise the caught exception
        # if any was caught
        if self.exc:
            raise self.exc
 
# Driver function
def main():
   
    # Create a new Thread t
    # Here Main is the caller thread
    t = MyThread()
    t.start()
     
    # Exception handled in Caller thread
    try:
        t.join()
    except Exception as e:
        print("Exception Handled in Main, Details of the Exception:", e)
 
# Driver code
if __name__ == '__main__':
    main()


输出:

Hello World

为了在调用者线程中捕获和处理线程的异常,我们使用一个变量来存储被调用线程中引发的异常(如果有),当被调用线程被连接时,连接函数检查 exc 的值是否为 None,如果则不生成异常,否则再次引发存储在 exc 中的生成异常。这发生在调用者线程中,因此可以在调用者线程本身中处理。

示例:该示例创建了一个 MyThread 类型的线程 t,该线程的 run() 方法调用了 someFunction() 方法,该方法引发了 MyException,因此每当线程运行时,它都会引发异常。为了在调用者线程中捕获异常,我们维护了一个单独的变量 exc,它设置为当被调用线程引发异常时引发的异常。最后在 join() 方法中检查此 exc,如果不是 None,则 join 只会引发相同的异常。因此,在调用者线程中引发了捕获的异常,因为 join 在调用者线程(这里是主线程)中返回,因此被相应地处理。

Python3

# Importing the modules
import threading
import sys
 
# Custom Exception Class
class MyException(Exception):
    pass
 
# Custom Thread Class
class MyThread(threading.Thread):
     
  # Function that raises the custom exception
    def someFunction(self):
        name = threading.current_thread().name
        raise MyException("An error in thread "+ name)
 
    def run(self):
       
        # Variable that stores the exception, if raised by someFunction
        self.exc = None           
        try:
            self.someFunction()
        except BaseException as e:
            self.exc = e
       
    def join(self):
        threading.Thread.join(self)
        # Since join() returns in caller thread
        # we re-raise the caught exception
        # if any was caught
        if self.exc:
            raise self.exc
 
# Driver function
def main():
   
    # Create a new Thread t
    # Here Main is the caller thread
    t = MyThread()
    t.start()
     
    # Exception handled in Caller thread
    try:
        t.join()
    except Exception as e:
        print("Exception Handled in Main, Details of the Exception:", e)
 
# Driver code
if __name__ == '__main__':
    main()

输出: