5

I'm trying to create an object but as a new process. I'm following this guide and came up with this code.

import multiprocessing as mp 
import time

class My_class(mp.Process):
    def run(self):
        print self.name, "created"
        time.sleep(10)
        print self.name, "exiting"
        self.x()

    def x(self):
        print self.name, "X"

if __name__ == '__main__':
    print 'main started'
    p1=My_class()
    p2=My_class()
    p1.start()
    p2.start()  
    print 'main exited'

But here I'm unable to pass arguments to the object. I searched but found none. It's not like a normal multiprocess where we'd be doing something like:

p1 = multiprocessing.Process(target=My_class, args=('p1',10,))

to pass the arguments to the new process. But the multiprocessing for a class instance is different. For now I'm passing it the hard way like below.

import multiprocessing as mp 
import time

class My_class(mp.Process):
    my_vars={'name':'', 'num':0}
    def run(self):
        self.name=My_class.my_vars['name']
        self.num=My_class.my_vars['num']
        print self.name, "created and waiting for", str(self.num), "seconds"
        time.sleep(self.num)
        print self.name, "exiting"
        self.x()

    def x(self):
        print self.name, "X"

if __name__ == '__main__':
    print 'main started'
    p1=My_class()
    p2=My_class()
    My_class.my_vars['name']='p1'
    My_class.my_vars['num']=20
    p1.start()
    My_class.my_vars['name']='p2'
    My_class.my_vars['num']=10
    p2.start()  
    print 'main exited'

Which is working fine. But I guess it may fail for complex arguments like a large list or object or something like that.

Is there any other method to pass the arguments??

RatDon
  • 3,403
  • 8
  • 43
  • 85
  • You can pass primitive arguments, but you won't be able to pass objects. Processes don't share memory. Take a look at this answer http://stackoverflow.com/questions/18114285/python-what-are-the-differences-between-the-threading-and-multiprocessing-modul – Boris Feb 25 '15 at 04:48
  • This one also has info you need. http://stackoverflow.com/questions/4063220/override-multiprocessing-in-python – Boris Feb 25 '15 at 04:51
  • @Boris Processes don't share memory. Here I'm not trying to share a variable among processes. But while creating the process, I'm trying to pass the arguments which is actually allowed in multiprocessing. – RatDon Feb 25 '15 at 04:58

1 Answers1

10

You can just implement an __init__ method for My_class, which takes the two parameters you want to provide:

import multiprocessing as mp 
import time

class My_class(mp.Process):
    def __init__(self, name, num):
        super(My_class, self).__init__()
        self.name = name
        self.num = num

    def run(self):
        print self.name, "created and waiting for", str(self.num), "seconds"
        time.sleep(self.num)
        print self.name, "exiting"
        self.x()

    def x(self):
        print self.name, "X"

if __name__ == '__main__':
    print 'main started'
    p1=My_class('p1', 20)
    p2=My_class('p2', 10)
    p1.start()
    p2.start()  
    print 'main exited'

You just need to be sure to call super(My_class, self).__init__() in your __init__ method, so that your class is properly initialized as a Process subclass.

dano
  • 91,354
  • 19
  • 222
  • 219