multiprocessing, what am I doing wrong?

MRAB python at
Mon Feb 27 21:38:26 EST 2012

On 27/02/2012 16:57, Eric Frederich wrote:
> Still freezing sometimes, like 1 out of 10 times that I run it.
> Here is updated code and a couple of outputs.
I don't know what the problem is. All I can suggest is a slightly
modified version.

If a worker that says it's terminating without first saying that it's
got nothing, then I can only assume that the worker had some uncaught

#!/usr/bin/env python

import sys
import Queue
import multiprocessing
import time

def FOO(a, b, c):
     print 'foo', a, b, c
     return (a + b) * c

class MyWorker(multiprocessing.Process):
     def __init__(self, name, inbox, outbox):
         super(MyWorker, self).__init__() = name
         self.inbox = inbox
         self.outbox = outbox
         print >> sys.stderr, 'Created %s' %; sys.stderr.flush()
     def run(self):
         print >> sys.stderr, 'Running %s' %; sys.stderr.flush()
             while True:
                     args = self.inbox.get_nowait()
                     print >> sys.stderr, '%s got something to do' %; sys.stderr.flush()
                 except Queue.Empty:
                     print >> sys.stderr, '%s got nothing' %; 
             print >> sys.stderr, '%s is terminating' %; 

if __name__ == '__main__':
     # This file is being run as the main script. This part won't be
     # run if the file is imported.

     print >> sys.stderr, 'Creating todo queue'; sys.stderr.flush()
     todo = multiprocessing.Queue()

     for i in xrange(100):
         todo.put((i, i + 1, i + 2))

     print >> sys.stderr, 'Creating results queue'; sys.stderr.flush()
     result_queue = multiprocessing.Queue()

     print >> sys.stderr, 'Creating Workers'; sys.stderr.flush()
     w1 = MyWorker('Worker 1', todo, result_queue)
     w2 = MyWorker('Worker 2', todo, result_queue)

     print >> sys.stderr, 'Starting Worker 1'; sys.stderr.flush()
     print >> sys.stderr, 'Starting Worker 2'; sys.stderr.flush()

     for i in xrange(100):
         print result_queue.get()

More information about the Python-list mailing list