Wednesday, 15 May 2019

Python ThreadPoolExecutor Suppress Exceptions

from concurrent.futures import ThreadPoolExecutor, wait, ALL_COMPLETED


def div_zero(x):
    print('In div_zero')
    return x / 0


with ThreadPoolExecutor(max_workers=4) as executor:
    futures = executor.submit(div_zero, 1)

    done, _ = wait([futures], return_when=ALL_COMPLETED)
    # print(done.pop().result())

    print('Done')

The program above will run to completion without any error message.

You can only get the exception if you explicitly call future.result() or future.exception(), like what I did in the line commented-out.

I wonder why this Python module chose this kind of behavior even if it hides problems. Because of this, I spent hours debugging a programming error (referencing a non-exist attribute in a class) that would otherwise be very obvious if the program just crashes with exception, like Java for instance.



from Python ThreadPoolExecutor Suppress Exceptions

No comments:

Post a Comment