Global variable increment and python multiprocessing

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

Hi I have following multiprocess code and I want to ensure that global variable ctr should get updated by all end nodes or leaves of decision tree. But it not happening.

    ctr=0

            def update(l,n):
               global ctr
               l.acquire()
               ctr+=n
               l.release()

    def func(x,i):
        p2=[]
        if i > 100:
           lock=Lock()
                        update(lock,len(rl))
        # create list a1
        # create list a2
        i=len(a1)
        for a in a1:
            for b in a2:
                if x > (a+b):
                    proc=Process(target=func,args=(a+b,i,))
                    p2.append(proc)

        for p in p2:
            p.start()
            p.join()

Answers

When you start new processes with Python (or any other language for that matter) the same memory segments are used by all processes for read access. However, as soon as you start writing to some memory (e.g. update a variable) that variable get s copied into the new processes own memory segment.

In other words, you can t update a global variable and expect multiple processes to see the same value, you need some kind of shared memory for that.

http://docs.python.org/3.3/library/multiprocessing.html#sharing-state-between-processes http://docs.python.org/3.3/library/multiprocessing.html#sharing-state-between-processes

Keep in mind though, that updating a global variable involves some amount of locking, basically forcing your multiple processes to behave in a serial manner. This can negatively impact performance depending on your use-case. You should try to avoid using global state whenever you can (this is more general advice, but in multiprocessing cases it s even worse to have global state.)

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/17080497/global-variable-increment-and-python-multiprocessing

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils