If I do a time.sleep(0.001) right at the beginning of the run() method, then it completes fine.<br>I was able to run it through a couple hundred times without problem.<br>If I sleep for less time than that or not at all, it may or may not complete.<br>
<br><div class="gmail_quote">On Mon, Feb 27, 2012 at 9:38 PM, MRAB <span dir="ltr"><<a href="mailto:python@mrabarnett.plus.com">python@mrabarnett.plus.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="im">On 27/02/2012 16:57, Eric Frederich wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Still freezing sometimes, like 1 out of 10 times that I run it.<br>
Here is updated code and a couple of outputs.<br>
<br>
</blockquote></div>
[snip]<br>
I don't know what the problem is. All I can suggest is a slightly<br>
modified version.<br>
<br>
If a worker that says it's terminating without first saying that it's<br>
got nothing, then I can only assume that the worker had some uncaught<br>
exception.<div class="im"><br>
<br>
<br>
#!/usr/bin/env python<br>
<br>
import sys<br>
import Queue<br>
import multiprocessing<br>
import time<br>
<br>
def FOO(a, b, c):<br>
    print 'foo', a, b, c<br>
    return (a + b) * c<br>
<br>
class MyWorker(multiprocessing.<u></u>Process):<br>
    def __init__(self, name, inbox, outbox):<br>
        super(MyWorker, self).__init__()<br>
        <a href="http://self.name" target="_blank">self.name</a> = name<br>
        self.inbox = inbox<br>
        self.outbox = outbox<br>
        print >> sys.stderr, 'Created %s' % <a href="http://self.name" target="_blank">self.name</a>; sys.stderr.flush()<br>
    def run(self):<br>
        print >> sys.stderr, 'Running %s' % <a href="http://self.name" target="_blank">self.name</a>; sys.stderr.flush()<br></div>
        try:<div class="im"><br>
            while True:<br>
                try:<br>
                    args = self.inbox.get_nowait()<br>
                    print >> sys.stderr, '%s got something to do' % <a href="http://self.name" target="_blank">self.name</a>; sys.stderr.flush()<br>
                except Queue.Empty:<br></div>
                    print >> sys.stderr, '%s got nothing' % <a href="http://self.name" target="_blank">self.name</a>; sys.stderr.flush()<br>
                    break<br>
                self.outbox.put(FOO(*args))<br>
        finally:<br>
            print >> sys.stderr, '%s is terminating' % <a href="http://self.name" target="_blank">self.name</a>; sys.stderr.flush()<div class="HOEnZb"><div class="h5"><br>
<br>
if __name__ == '__main__':<br>
    # This file is being run as the main script. This part won't be<br>
    # run if the file is imported.<br>
<br>
    print >> sys.stderr, 'Creating todo queue'; sys.stderr.flush()<br>
    todo = multiprocessing.Queue()<br>
<br>
    for i in xrange(100):<br>
        todo.put((i, i + 1, i + 2))<br>
<br>
    print >> sys.stderr, 'Creating results queue'; sys.stderr.flush()<br>
    result_queue = multiprocessing.Queue()<br>
<br>
    print >> sys.stderr, 'Creating Workers'; sys.stderr.flush()<br>
    w1 = MyWorker('Worker 1', todo, result_queue)<br>
    w2 = MyWorker('Worker 2', todo, result_queue)<br>
<br>
    print >> sys.stderr, 'Starting Worker 1'; sys.stderr.flush()<br>
    w1.start()<br>
    print >> sys.stderr, 'Starting Worker 2'; sys.stderr.flush()<br>
    w2.start()<br>
<br>
    for i in xrange(100):<br>
        print result_queue.get()<br></div></div><span class="HOEnZb"><font color="#888888">
-- <br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/<u></u>mailman/listinfo/python-list</a><br>
</font></span></blockquote></div><br>