In ParallelPython, a method of an object ( object.func() ) fails to manipulate a variable of an object ( object.value )

StackOverflow https://stackoverflow.com/questions/9941339

Question

With parallelpython, I am trying to convert my old serial code to parallel, which heavily relies on objects that have methods that change that object's variables. A stripped example in which I omit the syntax in favor of simplicity:

class Network:
    self.adjacency_matrix = [ ... ]
    self.state = [ ... ]
    self.equilibria = [ ... ]

...

   def populate_equilibria(self):
       # this function takes every possible value that self.state can be in
       # runs the boolean dynamical system 
       # and writes an integer within self.equilibria for each self.state
       # doesn't return anything

I call this method as: Code:

j1 = jobserver.submit(net2.populate_equilibria,(),(),("numpy as num"))

The job is sumbitted, and I know that a long computation takes place, so I speculate that my code is ran.

The problem is, i am new to parallelpython , I was expecting that, when the method is called, the variable net2.equilibria would be written accordingly, and I would get a revised object (net2) . That is how my code works, independent objects with methods that act upon the object's variables.

Rather, though the computation is apparent, and reasonably timed, the variable net2.equilibria remains unchanged. As if PP only takes the function and the object, computes it elsewhere, but never returns the object, so I am left with the old one.

What do I miss?

Thanks in advance.

Était-ce utile?

La solution

ParallelPython does not work this way. When you submit a function to the job server, you get a handle which you can call to get the result of the computation. This is the only way to get the result of your computation. Local objects are not modified by distant concurrent processes.

What you should do is wrap your method invocation inside a function which returns the new state of the object:

def populate_equilibria (obj):
    obj.populate_equilibria()
    return obj

j1 = jobserver.submit(populate_equilibria,(net2,),(),("numpy as num"))
net2 = j1()
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top