I am using the Python multiprocessing library. Whenever one of the processes throw a timeout error, my application ends itself. I want to keep the processes up.
I have a function that subscribes to a queue and listens to incoming messages:
def process_msg(i):
#get new message from the queue
#process it
import time
time.sleep(10)
return True
I have created a Pool that creates 6 processes and executes the process_msg() function above. When the function times out, I want the Pool to call the function again and wait for new messages instead of exiting:
if __name__ == "main":
import multiprocessing
from multiprocessing import Pool
pool = Pool(processes=6)
collection = range(6)
try:
val = pool.map_async(process_msg, collection)
try:
res = val.get(5)
except TimeoutError:
print('timeout here')
pool.close()
pool.terminate()
pool.join()
The code runs and when I get a timeout, the application terminates itself.
What I want it to do is to print that the timeout as occurred and call the same function again.
What's the right approach?