Threading Pool Event()

Graeme Matthew gsmatthew at ozemail.com.au
Sat Jul 19 05:55:44 CEST 2003


ok so code like this is perfectly safe

def run(self):

    while 1:

        job = queue.get()

        __processjob()

thanks




"Cliff Wells" <clifford.wells at comcast.net> wrote in message
news:mailman.1058582130.2101.python-list at python.org...
> On Fri, 2003-07-18 at 18:29, Graeme Matthew wrote:
> > ok still a bit confused sorry .... first attempt at a thread pool
> >
> > Am I correct in saying that each thread runs in a continious loop
>
> Yes.
>
> > each thread calls queue.get. The get method will block and not return
> > anything until an item is placed into it. When an item is placed into
> > it, one of the threads will get assinged a job i.e the first one that
> > happens to be in use during the cycle ?
>
> I think you have the idea, but I'm not sure (your terminology escapes me
> a bit toward the end).  What happens is each thread blocks waiting for
> something to be placed on the queue.  When something is put on the
> queue, one of the available threads (i.e. one that is blocking on
> queue.get() rather than processing a job).  Only a single thread will be
> woken for each item that is put on the queue.  It isn't knowable which
> thread that will be.
>
> > The job is returned to the thread, it runs with the job and does all
> > the processing then returns and calls queue.get again and waits for a
> > job to become available ?
>
> Yes.
>
> > When placing a job via Queue.put() one must acquire a lock place it
> > and then release it
>
> No.  The locking semantics are internal to the Queue object.  Do not
> concern yourself with it.  It simply works.  Do not attempt to put locks
> around it.
>
> > Am i aslo correct in saying that queue is actually doing the blocking
> > and controls which thread gets the job ?
>
> In a sense (that is, for practical purposes).  How it is done internally
> by the interpreter isn't important.
>
> > Lastly, sorry for all the questions, surely the CPU usage is the same
> > when the queue is waiting for jobs and when the threads are polling as
> > theyre all in one process anyway
>
> The threads are asleep while waiting.  They don't consume any CPU.  The
> queue doesn't "wait" for jobs (that is, it doesn't loop, poll or
> otherwise consume any CPU time), when you call queue.put() it is a
> method call on an object, not a thread.
>
> > Am i getting this or am i way off :-)
>
> Just a little way off ;)
>
> Regards,
>
> Cliff
>
> --
> My only regret is that I ever was born
>                                -Swans
>
>






More information about the Python-list mailing list