Multiprocessing / threading confusion

Paul Pittlerson menkomigen6 at gmail.com
Thu Sep 5 15:27:26 EDT 2013


I'm trying to understand data handling using multiprocessing and threading, haven't gotten very far without running into problems. This is my code:

#!/usr/bin/python

from multiprocessing import Process
from multiprocessing import Queue
from multiprocessing import current_process

from threading import Thread

import time

def gogo(qu):
    w = Worker(qu)
    w.start()

class Worker(Thread):
    def __init__(self, Que):
        super (Worker, self).__init__()
        
        self._pid = current_process().pid  
        
        self.que = Que        
        self.que.put('started worker %s' % self._pid)
    
    def run(self):
        self.que.put('%s ticked' % self._pid)
    
    def __del__(self):
        self.que.put('%s has exited' % self._pid)

class Debugger(Thread):
    def __init__(self, q):
        super(Debugger, self).__init__()
        
        self.q = q
        
    def run(self):
        while True:
            time.sleep(1)
            
            if not self.q.empty():
                print self.q.get()
            
            else: break;

if __name__ == '__main__':
    
    debug_q = Queue()
    
    debug = Debugger(debug_q)
    debug.start()
    
    for i in range(5):
        d = Process(target=gogo, args=(debug_q,))
        d.start()

What I expect to happen is the Debugger object will receive one string at a time, and read it from the queue. But that's not what I see the the output, the "started worker" stuff seems to print for every process, but "ticked" and "exited" will show up in unpredictable ways, I'm guessing they overwrite each other and therefore will not always appear in the output.

So I'm looking for help in trying to make sense of this kind of stuff, I thought this was the basic functionality that Queue() would take care of unless there is some other problem in my code.



More information about the Python-list mailing list