I am trying to exit a multiprocessing script when an error is called by the target function, but instead of leaving, the parent process just hangs.
This is a test script I use to replicate problems:
import time, multiprocessing as mp
def myWait(wait, resultQueue):
startedAt = time.strftime("%H:%M:%S", time.localtime())
time.sleep(wait)
endedAt = time.strftime("%H:%M:%S", time.localtime())
name = mp.current_process().name
resultQueue.put((name, wait, startedAt, endedAt))
resultQueue = mp.Queue()
proc = [
mp.Process(target=myWait, name = ' _One_', args=(2, resultQueue,)),
mp.Process(target=myWait, name = ' _Two_', args=(2, resultQueue,))
]
for p in proc:
p.start()
for p in proc:
p.join()
results = {}
for p in proc:
name, wait, startedAt, endedAt = resultQueue.get()
print('Process %s started at %s wait %s ended at %s' % (name, startedAt, wait, endedAt))
This works fine, I can see the parent script that spawns two child processes in htop, but when I want to force the parent script to exit, if the error in the target function, the myWaitparent process just hangs and does not even spawn the child process. I have ctrl-cto kill him.
def myWait(wait, resultQueue):
try:
except:
raise SystemExit
I have tried every way to get out of the function (eg, exit(), sys.exit(), os._exit()...) to no avail.