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>Ok, here's what I suppose the program will do:</p>

<ol>
<li>At <code>PowerSuppy(8000,'L1')</code>, it starts a subprocess and calls <code>self.print_time('tname','delay')</code>. Since <code>'delay'</code> is not a number it immediately raises a <code>TypeError</code> in the subprocess and end (so <code>self.P.join()</code> didn't block at all).</li>
<li>At <code>ps1.print_time('thread1',2)</code>, it runs the method in main process and is blocked until it end.</li>
<li><code>ps1.print_time('thread2',3)</code> does the same thing as the previous line did in main process.</li>
</ol>

<p>How to fix it:</p>

<ol>
<li>Do not initialize the subprocess in <code>__init__</code> method, instead, initialize it in <code>print_time</code> method.</li>
<li>Implemented a internal method for <code>target</code> function for subprocess.</li>
<li>Do not run <code>Process.join</code> unless you want to run the subprocess in sequence.</li>
</ol>

<p>Here's the code:</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"

    # ---------------------------------- #
    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):
        P = Process(target=self._print_time, args=(tname, delay))
        P.start()

    def _print_time(tname, delay):
        count = 0
        while count &lt; 5:
            time.sleep(delay)
        count += 1
        print "%s: %s"%(tname, time.ctime(time.time()))
</code></pre>

python oop python-multiprocessing