I have some code like this
import multiprocessing as mp
processes = []
for i in range(10):
p = mp.Process(target=my_func, args=args)
processes.append(p)
p.start()
for p in processes:
p.join()
If there is a bug in my_func
that causes all the threads to crash, then I want my parent process to detect that and also throw an exception. How can I detect if the process exited cleanly when I join the processes?
read the value of the process' exitcode
:
None
if the process has not finished yet. 0
if the process ended successfully. N
if the process had an error, and exited with code N. -N
if the process was killed with the signal N
. (eg. -15 if killed by SIGTERM
) For example, in your main process:
for p in processes:
p.join()
if p.exitcode > 0:
raise ValueError(f'a process exited with code {p.exitcode}')
And in your runnable:
try:
do_something()
except KnownError as _:
exit(my_known_error_code)
You can check exit code after calling join()
.
For example:
for p in processes:
p.join()
if p.exitcode is not 0:
exit(1)
Reference:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.