Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to stop all processes if one of them changes a global "stop" variable to True

import multiprocessing


global stop
stop = False


def makeprocesses():
    processes = []
    for _ in range(50):
        p = multiprocessing.Process(target=runprocess)
        
        processes.append(p)
    
    for _ in range(50):
        processes[_].start()
    runprocess()


def runprocess():
    global stop
    while stop == False:
        
        x = 1 #do something here

        if x = 1:
            stop = True




makeprocesses()

while stop == True:
    x = 0
    makeprocesses()

How could I make all the other 49 processes stop if just one changes stop to True?
I would think since stop is a global variable once one process changes stop all the others would stop.

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

No. Each process gets its own copy. It’s global to the script, but not across processes. Remember that each process has a completely separate address space. It gets a COPY of the first process’ data.

If you need to communicate across processes, you need to use one of the synchronization techniques in the multiprocessing documentation (https://docs.python.org/3/library/multiprocessing.html#synchronization-primitives), like an Event or a shared object.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading