tl;dr I spawn 3 threads, each thread throws an exception, most pythonic way to raise all 3 exceptions?
Below is a code example that is similar to what I am doing.
from multiprocessing.pool import ThreadPool
def fail_func(host):
raise Exception('{} FAILED!!!'.format(host))
hosts = ['172.1.1.1', '172.1.1.2', '172.1.1.3']
pool = ThreadPool(processes=5)
workers = [pool.apply_async(fail_func(host)) for host in hosts]
# join and close thread pool
pool.join(); pool.close()
# get the exceptions
[worker.get() for worker in workers if not worker.successful()]
What it ends up doing is just failing on the 1st host with the following traceback:
Traceback (most recent call last):
File "thread_exception_example.py", line 8, in <module>
workers = [pool.apply_async(fail_func(host)) for host in hosts]
File "thread_exception_example.py", line 4, in fail_func
raise Exception('{} FAILED!!!'.format(host))
Exception: 172.1.1.1 FAILED!!!
But what I want it to do is raise multiple exceptions for each thread that failed, like so:
Traceback (most recent call last):
File "thread_exception_example.py", line 8, in <module>
workers = [pool.apply_async(fail_func(host)) for host in hosts]
File "thread_exception_example.py", line 4, in fail_func
raise Exception('{} FAILED!!!'.format(host))
Exception: 172.1.1.1 FAILED!!!
Traceback (most recent call last):
File "thread_exception_example.py", line 8, in <module>
workers = [pool.apply_async(fail_func(host)) for host in hosts]
File "thread_exception_example.py", line 4, in fail_func
raise Exception('{} FAILED!!!'.format(host))
Exception: 172.1.1.2 FAILED!!!
Traceback (most recent call last):
File "thread_exception_example.py", line 8, in <module>
workers = [pool.apply_async(fail_func(host)) for host in hosts]
File "thread_exception_example.py", line 4, in fail_func
raise Exception('{} FAILED!!!'.format(host))
Exception: 172.1.1.3 FAILED!!!
is there any pythonic way of doing this? or do I need to wrap everything in a try/except, collect all the messages, then re-raise a single Exception?
worker. Normally, that word refers to the subprocesses in the pool.multiprocessing.pool.ThreadPoolis an undocumented feature, whilemultiprocessing.dummy.Poolis documented and should give you what you want. I personally think that in this case, the much clearer nameThreadPoolvs.dummy.Pooloutweighs that, but it's worth knowing the choice for yourself.