geek
geek

Reputation: 359

Python multiprocessing issue

I have a small piece of code, written in python which has a Generate class which returns the next number when ever called.

class Generate:
    def iter(self):
        self.next1 = 1
        return self
    def next(self):
        self.curr = self.next1
        self.next1 += 1
        return self.curr

def generateNos():
    for i in xrange(3):
        print g.next()

g = Generate().iter()

generateNos()
generateNos()

And a function generateNos, which generate 3 Nos only. So when i call this function two times as below, i get output as:- 1 2 3 4 5 6

Now, When I spawn two processes for calling this generateNos(), I get the output as:- 1 2 3 1 2 3

The code for the same is:-

from multiprocessing import Process, Manager
class Generate:
    def iter(self):
        self.next1 = 1
        return self
    def next(self):
        self.curr = self.next1
        self.next1 += 1
        return self.curr

def generateNos():
    for i in xrange(3):
        print g.next()

g = Generate().iter()

p1 = Process(target = generateNos)
p2 = Process(target = generateNos)

p1.start()
p1.join()

p2.start()
p2.join()

Now it seems this Generate class get re-initialized again in child process, which I don't want. How can i avoid this?

Wanted the same behavior as, calling generateNos twice one after another.

Any hack, so that Generate() object " g = Generate().iter()" doesn't gets called again in child proesses, It should be called only once in Parent Process.

Upvotes: 0

Views: 313

Answers (1)

Silas Ray
Silas Ray

Reputation: 26150

You have to have the object shared between processes via shared memory or some other mechanism that allows them to actually share the same object instance. Starting a new subprocess actually pickles everything you pass in to the child process then rebuilds a copy in the subprocess.

Upvotes: 2

Related Questions