Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python subclassing process with parameter

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

like image 275
RatDon Avatar asked Feb 25 '15 04:02

RatDon


People also ask

How to check if a class is a subclass in Python?

Python issubclass () This function is used to check whether a class is a subclass of another class. # superclass class Person(): pass # subclass class Employee(Person): pass print(issubclass(Person, Employee)) print(issubclass(Employee, Person))

How to start a new process or subprocess in Python?

To start a new process, or in other words, a new subprocess in Python, you need to use the Popen function call. It is possible to pass two parameters in the function call. The first parameter is the program you want to start, and the second is the file argument.

How do I pass two parameters to a function in Python?

It is possible to pass two parameters in the function call. The first parameter is the program you want to start, and the second is the file argument. In the example below, you will use Unix’s cat command and example.py as the two parameters.

What is the subprocess module in Python?

The subprocess module enables you to start new applications from your Python program. How cool is that? You can start a process in Python using the Popen function call. The program below starts the unix program ‘cat’ and the second parameter is the argument. This is equivalent to ‘cat test.py’.


1 Answers

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.

like image 187
dano Avatar answered Oct 12 '22 04:10

dano