3

I'm just starting with multiprocessing and i'm trying to share object between main and process. Code example:

import multiprocessing

class User(object):

  def __init__(self, name):
    self.name = name
    self.age = 0

  def getNameAndAge(self):
    return self.name + ' ' + str(self.age)

def define_age(user, age):
  user.age = age

bob = User('bob')

print bob.getNameAndAge()

define_age(bob, 25)

print bob.getNameAndAge()

p = multiprocessing.Process(target=define_age, args=(bob, 35))
p.start()
p.join()

print bob.getNameAndAge()

Output:

bob 0
bob 25
bob 25

How share bob object to get correct age ?

bux
  • 7,087
  • 11
  • 45
  • 86
  • In a far sense Managers could be of interest: http://docs.python.org/2/library/multiprocessing.html#using-a-remote-manager – User Apr 02 '13 at 12:36
  • @User it is not huge solution ? No simple "memory shared" solution ? (note: i've try with Proxy Objects without success) – bux Apr 02 '13 at 12:40
  • Where did you find proxy objects? – User Apr 02 '13 at 12:43
  • @User: http://docs.python.org/2/library/multiprocessing.html#proxy-objects – bux Apr 02 '13 at 12:48
  • This proxy would be exactly what I would look for. I have implemented an alternative you can try: https://github.com/niccokunzmann/pynet/tree/master/process the reference file is it. cou need to clone the whole repository or download it. This is an example: https://github.com/niccokunzmann/pynet/blob/master/documentation/done/tools.rst – User Apr 02 '13 at 13:24

0 Answers0