0

this is my code :

class tclass:
    value = 0
    lo = multiprocessing.Lock()
    def increase(self):
        print 'befor',self.value
        with self.lo:
            self.value+=1
        print 'after',self.value


tc = tclass()

def starttclassvalue():
    for i in range(10):
        tc.increase()

if __name__ == '__main__':
    multiprocessing.process.Process(target=starttclassvalue).start()
    multiprocessing.process.Process(target=starttclassvalue).start()
    multiprocessing.process.Process(target=starttclassvalue).start()
    multiprocessing.process.Process(target=starttclassvalue).start()

why value set to zero and start from zero ?

out :

befor 0 after 1 befor 1 after 2 befor 2 after 3 befor 3 after 4 befor 4 after 5 befor 5 after 6 befor 6 after 7 befor 7 after 8 befor 8 after 9 befor 9 after 10 befor 0 after 1 befor 1 after 2 befor 2 after 3 befor 3 after 4 befor 4 after 5 befor 5 after 6 befor 6 after 7 befor 7 after 8 befor 8 after 9 befor 9 after 10 befor 0 after 1 befor 1 after 2 befor 2 after 3 befor 3 after 4 befor 4 after 5 befor 5 after 6 befor 6 after 7 befor 7 after 8 befor 8 after 9 befor 9 after 10 befor 0 after 1 befor 1 after 2 befor 2 after 3 befor 3 after 4 befor 4 after 5 befor 5 after 6 befor 6 after 7 befor 7 after 8 befor 8 after 9 befor 9 after 10

why value in tclass is not 30 at end program ?

abbas-h
  • 392
  • 1
  • 4
  • 18
  • 1
    The code you posted isn't doing that (you can clearly see `'hci1': 1` in the output). Since you immediately return from the function at that point, the rest of the function cannot be responsible. Conclusion, **something else** must be doing that. You didn't post anything else here. – Martijn Pieters Dec 23 '14 at 08:18
  • Note that you didn't mark your `print` statements with any other information. Add a print statement entering the function (`print "entering GetFreeBluetoothhciid"` for example), or which of the existing `print` statements is which (`print 'HCI_STATUS before increment', self.HCI_STATUS` and `print 'HCI_STATUS after increment', self.HCI_STATUS`), so this may not be immediately obvious to you here. – Martijn Pieters Dec 23 '14 at 08:21
  • i edit and write simple code – abbas-h Dec 23 '14 at 09:09
  • Each process gets its own copy. Your dictionary isn't being reset, it is *not being shared*. – Martijn Pieters Dec 23 '14 at 09:11
  • Try removing `value = 0` and adding this to your class: `def __init__(self): self.value = 0` . I think the problem you are having is that the variable `value` is shared among class instances, with the replaced code you give each instance its own `value`, as you expected. – sentiao Dec 23 '14 at 14:49

1 Answers1

1

A new process is created with a COPY of the memory of the parent process. Changes to the memory of any child processes do not affect the parent. That memory is not shared. You could achieve what you want by using threads (which share the same memory of the parent process) instead of the multi processing library.

aychedee
  • 24,871
  • 8
  • 79
  • 83