Search code examples
pythonpython-multiprocessingpython-multithreading

How to create dependency between two parallel processes running simultaneously


I have two parallel processes running simultaneously, actually one process shows a user interface and another one runs script in the backend. If the user closes the window, i also want to stop the backend script from running.

Parallel processing code is shown here:

def run_parallel():
    p1 = Process(target=new_sniff_window)
    p1.start()
    p2 = Process(target=new_sniff)
    p2.start()
    p1.join()
    p2.join()

How can i abort process p2 when p1 exits?


Solution

  • You can run p2 as a deamon process

    p2.daemon = True
    

    p2 will terminate, if the mean program terminates. Or if p2 is a child process of p1, it will terminate if p1 terminates

    https://docs.python.org/3/library/multiprocessing.html#multiprocessing.Process.daemon