python multiprocessor how to stop all threads when on one of them find the result?
Question: I am trying to run my code in parallel using the python "from multiprocessing
import Process, Value" model.However, I am creating a shared variable and
using it as flag, so if one process find the result it will set the flag value
to 1 and exit.Other processes should exit when the flag value is changed but
for some reason the flag value did not change for these processes. this is the
code:

    
    
    from multiprocessing import Process, Value
    gflag= Value('i',0)#this is the global flag shared between process
    gflag=0
    while True:                                                                                                                                                                      
        if not piece:                                                                                                                                                                  
            break                                                                                                                                                                      
    
        list1= piece.splitlines()                                                                                                                                                         
    
        p = Process(target=Dowork, args=(gflag,inValue,list1,var1,))                                                                                                                     
    
        p.start()                                                                                                                                                                      
        if(gflag==1):                                                                                                                                                                  
                exit()                                                                                                                                                                 
    
        piece = f.read(10000)
    
    def doWork(gflag,inputvalue,diclist,var1):
        for p in diclist:
            calResult= doSomeCal(p,var1)
            if( calResult == inputvalue):
                gflag=1
                exit()
    
        if(gflag==1):
            print"exit now"
            exit()
    

Question is how to stop all the threads when on of them find the result?


Answer: The main problem with your attempted solution is that, because you assign to
`gflag` within `doWork`, it's a local variable, not a global. To fix that, you
need to add `global gflag` at the start of the function.

But even if you fixed that, it wouldn't solve your problem. When you write
`gflag=1`, that doesn't update the shared value stored in `gflag`, it just
rebinds the name `gflag` to the local int `1`. You want to use `gflag.value`.
(Note that once you fix this, the previous problem goes away… but it's still
often better to use the `global` declaration for human readers, even when the
compiler doesn't need it.)

And finally, `doWork` doesn't actually check the flag until after it's
finished doing all the work, so the flag doesn't help you to exit the child
processes earlier. You probably want to put that `if` statement inside the
`for` loop, so it checks the flag once per element, instead of only once at
the end.

