I am writing a python script which has 2 child processes. The main logic occurs in one process and another process waits for some time and then kills the main process even if the logic is not done.
I read that calling os_exit(1) stops the interpreter, so the entire script is killed automatically. I've used it like shown below:
import os from multiprocessing import Process, Lock from multiprocessing.sharedctypes import Array # Main process def main_process(shared_variable): shared_variable.value = "mainprc" time.sleep(20) print("Task finished normally.") os._exit(1) # Timer process def timer_process(shared_variable): threshold_time_secs = 5 time.sleep(threshold_time_secs) print("Timeout reached") print("Shared variable ",shared_variable.value) print("Task is shutdown.") os._exit(1) if __name__ == "__main__": lock = Lock() shared_variable = Array('c',"initial",lock=lock) process_main = Process(target=main_process, args=(shared_variable)) process_timer = Process(target=timer_process, args=(shared_variable)) process_main.start() process_timer.start() process_timer.join()
The timer process calls os._exit but the script still waits for the main process to print
"Task finished normally." before exiting.
How do I make it such that if timer process exits, the entire program is shutdown (including main process)?