Hello this is a sample example of something that I want to do. I want to use threads in my application. It's working fine the issues is python is not throwing error when I by mistake call function with wrong parameter making it sometimes harder to debug.
Sample program : as you can see I am calling console_log
function with wrong number of arguments. When I run the program there is no output and it exits
from multiprocessing.pool import ThreadPool
def console_log(id, msg):
print("{} : {}".format(id, msg))
data = [1,2,3,4,5,6]
max_threads = 1
pool = ThreadPool(processes=max_threads)
for d in data:
pool.apply_async(console_log, (d))
pool.close()
pool.join()
Same program but without threads and pools
def console_log(id, msg):
print("{} : {}".format(id, msg))
data = [1,2,3,4,5,6]
for d in data:
console_log(d)
output
Traceback (most recent call last):
File "async.py", line 12, in <module>
console_log(d)
TypeError: console_log() missing 1 required positional argument: 'msg'
Why don't I get same error output when I use threadpool ? Is there a way to get that error message. I am using python version 3.8.10
UPDATE error_callback can be used to return the error. So the function call becomes something like this
def callback_error(result):
print('error', result)
pool.apply_async(console_log, (d,), error_callback=callback_error)
I found it through question suggestions. Link : Exception thrown in multiprocessing Pool not detected