Python multiprocessing and an imported module -


i have 2 processing running access imported module that:

import foo  def bar():   while true:     foo.a = true  def baz():   while true:     print foo.a  p1 = process(target=bar) p2 = process(target=baz) p1.start() p2.start() 

it seems each process have own instance of module foo, bar() changes value true, in baz() it's false. workaround?

unlike threads, separate processes not share memory. there ways, however, to share data between separate processes. 1 way use mp.value:

foo.py:

import multiprocessing mp = mp.value('b', false) 

then script

import time import foo import multiprocessing mp  def bar():     foo.a.value = true  def baz():     in range(10**5):         print foo.a.value  if __name__ == '__main__':     p2 = mp.process(target=baz)     p2.start()     time.sleep(0.5)     p1 = mp.process(target=bar)     p1.start() 

yields

0 0 0 ... 1 1 1 ... 

for sharing boolean value, mp.event perhaps better option:

foo.py:

import multiprocessing mp = mp.event() 

script.py:

def bar():     foo.a.set()  def baz():     in range(10**5):         print foo.a.is_set() 

yields

false false false ... true true true ...  

Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

c# - two queries in same method -