How to use Multiprocessing in Python within a class
<p>What I'd like to do is use multiprocessing on one of my class methods. I have tried to follow the example in the Python help file but am not getting the result expected. Here is my class file:</p>

<pre><code>import os
import telnetlib

class PowerSupply():

    # ---------------------------------- #
    def __init__(self,port_no,my_name):
        self.Status = "Off"
        self.Port = port_no
        self.Name = my_name
        self.tn = None
        self.HOST = "192"
        self.P = Process(target=self.print_time, args=('tname','delay')
        self.P.start()
        self.P.join()
    # ---------------------------------- #
    def TurnOn(self):
        onCommand = "OUT 1\r"

        if self.Status == "ON":
            print "I'm already on"
        else:
            self.tn = telnetlib.Telnet(self.HOST,self.Port)
            self.tn.write(onCommand)
            self.Status = "ON"
            print "I am now on"
    # ---------------------------------- #
    def TurnOff(self):
        offCommand = "OUT 0\r"
        self.tn.write(offCommand)
        self.tn.close()
        print "I am now off"
    # ---------------------------------- #
    def SetVoltage(self,volts):
        voltageCommand = "PV" + " " + str(volts) + "\r"
        self.tn.write(voltageCommand)
    # ---------------------------------- #  
    def GetAllData(self):
        while(self.Status == "ON"):
            self.tn.write("DVC?\r")
            all_data = self.tn.read_some()
            vdc = all_data.split(',')
            vdc = vdc[0]
            print vdc
    # ---------------------------------- #
    def print_time(self, tname, delay):
       count = 0
       while count &lt; 5:
        time.sleep(delay)
        count += 1
        print "%s: %s"%(tname, time.ctime(time.time()))
</code></pre>

<p>Here is how I try to use the implementation:</p>

<pre><code>ps1 = PowerSuppy(8000,'L1')
ps1.print_time('thread1',2)
ps1.print_time('thread2',3)
</code></pre>

<p>When I try to use it as above, it still uses a procedural approach and doesn't call the thread2 until thread1 is complete. What is it exactly that I am doing wrong and how can I fix it?</p>

<p>Try to add a function in your class:</p>

<pre><code>def print_time_subprocess(self, tname, delay):
    p = Process(target=self.print_time, args=('tname','delay'))
    p.start()
</code></pre>

<p>and use this to test:</p>

<pre><code>ps1 = PowerSupply(8000,'L1')
ps1.print_time_subprocess('thread1',2)
ps1.print_time_subprocess('thread2',3)
</code></pre>

<p>and do not forget import <code>Process</code></p>

python oop python-multiprocessing