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??

3
  • 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 stackoverflow.com/questions/18114285/… Commented Feb 25, 2015 at 4:48
  • This one also has info you need. stackoverflow.com/questions/4063220/… Commented Feb 25, 2015 at 4: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. Commented Feb 25, 2015 at 4:58

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.