Multiprocessing / threading confusion

marduk at python.net marduk at python.net
Thu Sep 5 18:28:39 EDT 2013



On Thu, Sep 5, 2013, at 03:27 PM, Paul Pittlerson wrote:
> I'm trying to understand data handling using multiprocessing and
> threading, haven't gotten very far without running into problems. This is
> my code:

[snip (not sure why you are using multiprocessing and threading at the
same time]


> 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.

My output is probably totally different than your output.  I only get
the processes starting.  Here's why:  This stuff all runs
asynchronously.  When you start the "Debugger" thread.. I see you put a
sleep() in it, but that guarantees nothing.  At least on my machine
which is somewhat loaded ATM, by the time the Processes are started, the
Debugger thread has already finished (because of the check to see if the
queue was empty).  Apparently it is took longer than 1 second from the
time the Debugger was started and the first Process was started.
Likewise, what you are getting is probably a case where the queue is
momentarily empty by the time the debugger loop gets ahold of the queue
lock and checks to see if it's empty.  Therefore the Debugger quits. 
Also because of the asynchronicity of processes, threads, you can not
guarantee the order that the processes will get the opportunity to put()
into the queue. 

Also you can't (and shouldn't) depend on the time that __del__ gets
called.  It can get called at any time, in any order and sometimes not
at all.*

Hope this helps.

*
http://docs.python.org/3/reference/datamodel.html?highlight=__del__#object.__del__



More information about the Python-list mailing list