Python thread pool that handles exceptions Python thread pool that handles exceptions multithreading multithreading

Python thread pool that handles exceptions


I personally use Futures as the interface is very simple. For the traceback issue I found a workaround to preserve it. Checkout my answer to this other question:

Getting original line number for exception in concurrent.futures


If you want to get inforamtion about unhandled exception in threads and you use ThreadPoolExecutor, you can do like this:

import timeimport tracebackfrom concurrent.futures import ThreadPoolExecutordef worker():    a = 2 / 0def worker_callbacks(f):    e = f.exception()    if e is None:        return    trace = []    tb = e.__traceback__    while tb is not None:        trace.append({            "filename": tb.tb_frame.f_code.co_filename,            "name": tb.tb_frame.f_code.co_name,            "lineno": tb.tb_lineno        })        tb = tb.tb_next    print(str({        'type': type(e).__name__,        'message': str(e),        'trace': trace    }))executor = ThreadPoolExecutor(max_workers=1)executor.submit(worker).add_done_callback(worker_callbacks)


Easy solution: use whatever alternative suits you best, and implement your own try-except block in your workers. Surround the root call if you must.

I wouldn't say these libraries handle exceptions "incorrectly". They have a default behavior, however primitive. You are expected to handle this yourself if defaults don't suit you.