Search code examples
pythonpython-3.xconcurrent.futures

How to stop all threads if there is an exception in one thread?


I am currently running a function using python's concurrent.futures library. It looks like this (I am using Python 3.10.1 ):

with concurrent.futures.ThreadPoolExecutor() as executor:

future_results = [executor.submit(f.get_pdf_multi_thread, ssn) for ssn in ssns]

for future in concurrent.futures.as_completed(future_results):
    try:
        future.result()
    except Exception as exc:
        # If there is one exception in a thread stop all threads
        for future in future_results:
            future.cancel()
        raise exc

The aim of this is that, if there is any exception in one of the threads, stop the remaining ones and throw exception. However I don't know if this is doing what it's supposed to do (sometimes it takes a lot of time to throw the exception that I desire and some other times it throws it quickly). Could you help me with this? Thank you


Solution

  • Following Olvin Roght advice I've successfully managed to complete the task

    list(executor.map(f.get_pdf_multi_thread, ssns))