0

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

fist ace
  • 41
  • 1
  • 6
  • See also: [How to catch exceptions in workers in Multiprocessing](https://stackoverflow.com/questions/22094852/how-to-catch-exceptions-in-workers-in-multiprocessing) – Kemp Nov 26 '21 at 13:55
  • I have updated my question with the answer. Thanks for the link. I found error_callback to be right approach for me – fist ace Nov 26 '21 at 14:13

1 Answers1

0

You might need to do

for d in data:
    pool.apply_async(console_log, (d,))

I know in the past it's complained if I didn't add that comma