Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Simple question about Queue.Queue and threads
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  5 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Frank Millman  
View profile  
 More options Feb 5 2010, 7:45 am
Newsgroups: comp.lang.python
From: "Frank Millman" <fr...@chagford.com>
Date: Fri, 5 Feb 2010 14:45:30 +0200
Local: Fri, Feb 5 2010 7:45 am
Subject: Simple question about Queue.Queue and threads
Hi all

Assume you have a server process running, a pool of worker threads to
perform tasks, and a Queue.Queue() to pass the tasks to the workers.

In order to shut down the server cleanly, you want to ensure that the
workers have all finished their tasks. I like the technique of putting a
None onto the queue, and have each worker check for None, put None back onto
the queue, and terminate itself.

The main program would look something like this -

    q.put(None)
    for worker in worker_threads:
        worker.join()

At this point you can be sure that each thread has completed its tasks and
terminated itself.

However, the queue is not empty - it still has the final None in it.

Is it advisable to finalise the cleanup like this? -

    while not q.empty():
        q.get()
        q.task_done()
    q.join()

Or is this completely redundant?

Thanks

Frank Millman


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Gabriel Genellina  
View profile  
 More options Feb 6 2010, 12:59 am
Newsgroups: comp.lang.python
From: "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
Date: Sat, 06 Feb 2010 02:59:35 -0300
Local: Sat, Feb 6 2010 12:59 am
Subject: Re: Simple question about Queue.Queue and threads
En Fri, 05 Feb 2010 09:45:30 -0300, Frank Millman <fr...@chagford.com>  
escribi :

Yes - but who cares? :)

> Is it advisable to finalise the cleanup like this? -

>     while not q.empty():
>         q.get()
>         q.task_done()
>     q.join()

> Or is this completely redundant?

I don't see what you gain by doing that.

On the other hand, you might check whether the queue contains exactly one  
item and it is None, just to be sure that all queued work has been done.

(BTW, I'd use a different sentinel instead of None; perhaps this could not  
happen in your current code, but it's easy to inadvertedly put a None in  
the queue, stopping all workers prematurely.)

--
Gabriel Genellina


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Frank Millman  
View profile  
 More options Feb 6 2010, 1:30 am
Newsgroups: comp.lang.python
From: "Frank Millman" <fr...@chagford.com>
Date: Sat, 6 Feb 2010 08:30:55 +0200
Local: Sat, Feb 6 2010 1:30 am
Subject: Re: Simple question about Queue.Queue and threads
On Feb 6, 7:59 am, "Gabriel Genellina" <gagsl-...@yahoo.com.ar> wrote:

> En Fri, 05 Feb 2010 09:45:30 -0300, Frank Millman <fr...@chagford.com>
> escribi :

[...]
> > However, the queue is not empty - it still has the final None in it.

> Yes - but who cares? :)

That was my point. I didn't think I needed to care, but I wanted to be sure
I was not missing something.

I will just discard all the final cleanup code.

> (BTW, I'd use a different sentinel instead of None; perhaps this could not
> happen in your current code, but it's easy to inadvertedly put a None in
> the queue, stopping all workers prematurely.)

Good advice.

Thanks, Gabriel

Frank


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Steven  
View profile  
 More options Feb 8 2010, 9:51 am
Newsgroups: comp.lang.python
From: Steven <Steven.Rumbal...@fiserv.com>
Date: Mon, 8 Feb 2010 06:51:02 -0800 (PST)
Local: Mon, Feb 8 2010 9:51 am
Subject: Re: Simple question about Queue.Queue and threads
On Feb 5, 7:45 am, "Frank Millman" <fr...@chagford.com> wrote:

Queue objects have support for this signaling baked in with
q.task_done and q.join.

After the server process has put all tasks into the queue, it can join
the queue itself, not the worker threads.

q.join()

This will block until all tasks have been gotten AND completed.  The
worker threads would simply do this:
task_data = q.get()
do_task(task_data)
q.task_done()

Using pairs of get and task_done you no longer need to send a signal.
Just exit the server process and the worker threads will die (assuming
of course, you set .setDaemon(True) before starting each worker
thread).

Steven Rumbalski


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Frank Millman  
View profile  
 More options Feb 9 2010, 1:45 am
Newsgroups: comp.lang.python
From: "Frank Millman" <fr...@chagford.com>
Date: Tue, 9 Feb 2010 08:45:03 +0200
Local: Tues, Feb 9 2010 1:45 am
Subject: Re: Simple question about Queue.Queue and threads
On Feb 8, 4:51 pm, Steven <Steven.Rumbal...@fiserv.com> wrote:

Thanks, Steven.

This works perfectly in my scenario, and tidies up the code a bit.

Minor point - according to the 2.6 docs, .setDaemon(True) is the old API -
the current way of specifying this is .daemon = True.

Thanks for the tip.

Frank


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »