Producer/consumer Queue "trick"

Evan Simpson evan at tokenexchange.com
Mon Jan 17 12:56:07 EST 2005


Jon Perez wrote:
> If the consumer and the producer are separate threads,
> why does the consumer thread block when the producer
> thread is generating a new board?  Or why does it
> take forever for the producer thread to be pre-empted?
> 
> Also, I don't understand why the solution works.
> How does sleeping for .001 seconds right after putting
> a new board on the queue solve the problem?

I'm guessing at how things work, and may be totally wrong, but here's 
what I think happens:  In the Queue get() code, the consumer releases 
the 'fsema' lock. Directly or indirectly, this wakes up and hands 
control to the producer thread, which was blocked trying to acquire 
'fsema'. The sleep() hands control back to the scheduler immediately, 
which appears to wake up the consumer and let it get on with things.

It doesn't take "forever" for the producer to be preempted, just the 
normal preemption interval.  I was bothered, though,  by the idea of 
having it take even a few dozen milliseconds out of the middle of a 
request that's at most a millisecond or two away from finishing anyway.

Cheers,

Evan @ 4-am




More information about the Python-list mailing list