Python catch exception in thread

bsphere / py_exceptions.py

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

import Queue
import threading
class WorkerThread ( threading . Thread ):
def __init__ ( self , q ):
super ( WorkerThread , self ). __init__ ()
self q = q
self . exception = None
def run ():
try :
while not self . q . empty ():
item = None
try :
item = self . q . get ( False )
# do your stuff
except Queue . Empty :
pass
finally :
if item :
self . q . task_done ()
except Exception as e :
self . exception = e
def get_exception ():
return self . exception
class MyClass :
def spawn_threads ( self , q ):
self . threads = []
for _ in range ( NUM_THREADS ):
t = WorkerThread ( q )
self . threads , append ( t )
t . start ()
def raise_thread_exceptions ():
for t in self . threads :
e = t . get_exception ()
if e :
raise e
def my_func ( self ):
q = Queue . Queue ()
# do something and q.put()
self . spawn_threads ( q )
q . join ()
# raise exceptions from worker threads
self . raise_thread_exceptions ()

Источник

Handle Unexpected Exceptions in Threads with excepthook

You can handle unexpected exceptions in a thread by using an excepthook.

In this tutorial you will discover how to handle unexpected exceptions in Python threads

Need to Handle Unexpected Exceptions in Threads

A thread is a thread of execution in a computer program.

Читайте также:  Готовая социальная сеть html

Every Python program has at least one thread of execution called the main thread. Both processes and threads are created and managed by the underlying operating system.

Sometimes we may need to create additional threads in our program in order to execute code concurrently.

Python provides the ability to create and manage new threads via the threading module and the threading.Thread class.

It is possible for new threads to raise an unexpected exception while executing. This has a catastrophic effect on the new thread, unwinding the stack of function calls and ultimately stopping the thread from running any further.

Ideally, we would like to know when an unexpected exception occurs in new threads so that we might take appropriate action to clean-up any resources and perhaps log the fault.

How can we handle unexpected exceptions in new Python threads?

Run your loops using all CPUs, download my FREE book to learn how.

How to Handle Unexpected Exceptions in Threads

We can specify how to handle unhandled errors and exceptions that occur within new threads via the exception hook, referred to as “excepthook“.

By default, there is no exception hook, in which case the sys.excepthook function is called that reports the familiar message.

We can specify a custom exception hook function that will be called whenever a threading.Thread fails with an unhandled Error or Exception.

First, we must define a function that takes a single argument that will be an instance of the ExceptHookArgs class, containing details of the exception and thread.

Источник

How to Handle Exceptions in Tasks With the ThreadPoolExecutor in Python

You can handle exceptions in the ThreadPoolExecutor raised by tasks by catching them when getting the result.

In this tutorial, you will discover how to handle exceptions in the ThreadPoolExecutor.

Need to Handle Exceptions Raised by Tasks

The ThreadPoolExecutor in Python provides a pool of reusable threads for executing ad hoc tasks.

You can submit tasks to the thread pool by calling the submit ( ) function and passing in the name of the function you wish to execute on another thread.

Calling the submit ( ) function will return a Future object that allows you to check on the status of the task and get the result from the task once it completes.

You can also submit tasks by calling the map ( ) function and specifying the name of the function to execute and the iterable of items to which your function will be applied.

An exception raised in a single threaded program must be caught, otherwise it will unwind the main thread.

Unwinding a thread means that the exception is propagated up to the top of the stack of function calls until it reaches the top level at which it will cause the main thread to exit and close the program.

The ThreadPoolExecutor can be used to execute arbitrary functions.

Each task submitted to the thread pool is picked-up by a worker thread and executed. While executing, the function may raise an exception.

If uncaught, does the exception may unwind the worker thread and it permanently unavailable in the thread pool?

Additionally, we may need to know if a task raised an exception when getting the result so that we can take appropriate action, such as submit a follow-up task or perhaps re-try the task.

How do you handle the exception raised by a task in a ThreadPoolExecutor?

Run your loops using all CPUs, download my FREE book to learn how.

How to Handle Exceptions in Tasks

An exception raised by a target function executed by a worker thread will not unwind the worker thread.

The thread pool is robust to target functions that raise exceptions that are not caught. The exception will be caught by the thread pool automatically, and the worker thread will remain available for executing tasks.

There are three ways you may handle exceptions raised by a target function, they are:

  1. Handle exceptions when getting the task result.
  2. Check for raised exceptions.
  3. Handle exceptions within the task function.

Let’s take a closer look at each in turn.

Handle Exceptions When Getting the Result

You can handle exceptions raised within a task when getting the result from the task.

Recall that when you submit a task to the thread pool, you will receive a Future object in return.

When you wish to retrieve the result from the task, you can call the result ( ) function in the Future for the task.

If the task is completed successfully, it will return the result from your function or None if your function does not return a result.

If your target task function raises an exception during execution, the exception will be raised again (re-raised) when calling the result ( ) function, where you may need to handle it.

Источник

Оцените статью