2

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()

1 Answer 1

1

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.

Check out the documentation on Multiprocessing

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.)

Sign up to request clarification or add additional context in comments.

2 Comments

I have edited the code with lock. but it is still not working.
What I was saying is that you need to use some kind of shared memory mechanism, like a multiprocessing.Value, this is going to do the locking for you, so you don't have to. Just be aware that locking comes with a performance penalty.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.