I have a use case where I want my script that runs multiple functions parallelly using multiprocessing to exit with non zero code and that should stop running the script immediately on seeing the return code 1 or errors from any of the functions.
Here is what I am trying to do, not sure if my approach is correct and what should I do further.
def a(a1):
if a1==a2:
print("success")
return 0
else:
print("failure")
return 1 #or sys.exit(1) - not sure which 1 should I use here
def b(b1):
if b1==b2:
print("success")
return 0
else:
print("failure")
return 1 #or sys.exit(1) - not sure which 1 should I use here
def runall():
if __name__ =='__main__':
t1= multiprocess.Process(target=a, args=a1)
t1= multiprocess.Process(target=b, args=b1)
t1.start()
t1.join()
To exit completely you can just use sys.exit()
without argument. Just the documentation says:
The sys.exit() function allows the developer to exit from Python. The exit function takes an optional argument, typically an integer, that gives an exit status. Zero is considered a “successful termination”. Be sure to check if your operating system has any special meanings for its exit statuses so that you can follow them in your own application. Note that when you call exit, it will raise the SystemExit exception, which allows cleanup functions to work in the finally clauses of try / except blocks.
If you want to know which function terminated the program you can use different integer.