Search code examples
pythonconcurrencypython-multiprocessingpython-multithreadingthreadpoolexecutor

Why ThreadPoolExecutor ignoring exceptions?


I have a code like this:

from concurrent.futures.thread import ThreadPoolExecutor
from time import sleep


def foo(message, time):
    sleep(time)
    print('Before exception in ' + message)
    raise Exception('OOOPS! EXCEPTION IN ' + message)
    
if __name__ == '__main__':
    executor = ThreadPoolExecutor(max_workers=2)
    future1 = executor.submit(foo, 'first', 1)
    future2 = executor.submit(foo, 'second', 3)
    print(future1.result())
    print(future2.result())

And when I run this, I get the following as a result:

Before exception in first
Traceback (most recent call last):
  ...
  File "...", line 8, in foo
    raise Exception('OOOPS! EXCEPTION IN ' + message)
Exception: OOOPS! EXCEPTION IN first
Before exception in second

Process finished with exit code 1

Why is the second exception being ignored? I want to catch and handle exceptions on each of the threads


Solution

  • Because exception is raised by result() and prevents normal script flow. To see the second exception you need to catch the first one:

        executor = ThreadPoolExecutor(max_workers=2)
        future1 = executor.submit(foo, 'first', 1)
        future2 = executor.submit(foo, 'second', 3)
        try:
            print(future1.result())
        except:
            pass
        print(future2.result())