Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how does a queue stop the thread?

463 views
Skip to first unread message

kaiix

unread,
Apr 21, 2010, 4:08:20 AM4/21/10
to
A simple thread pool example. My question is, since *MyThread.run*
will loop endless, how does the thread know the time to quit? how does
the *queue* notify the thread? is there any shared variables, like a
*lock*?

When I set daemon false, it stays in the loop, not quit any more.
what's the role does the daemon state plays?

code:
----------------------------------------------------------------------------------------
import Queue
import threading

def do_some_thing(x):
print int(x)

class MyThread(threading.Thread):
def __init__(self, queue):
threading.Thread.__init__(self)
self.queue = queue

def run(self):
while True:
params = self.queue.get()
do_some_thing(params)
self.queue.task_done()

q = Queue.Queue()

for i in range(1, 5):
t = MyThread(q)
t.setDaemon(True)
t.start()

for x in range(10):
q.put(x)

q.join()

Kushal Kumaran

unread,
Apr 21, 2010, 4:20:15 AM4/21/10
to kaiix, pytho...@python.org
On Wed, Apr 21, 2010 at 1:38 PM, kaiix <kvn...@gmail.com> wrote:
> A simple thread pool example. My question is, since *MyThread.run*
> will loop endless, how does the thread know the time to quit? how does
> the *queue* notify the thread? is there any shared variables, like a
> *lock*?
>
> When I set daemon false, it stays in the loop, not quit any more.
> what's the role does the daemon state plays?
>

Take a look at the documentation for the threading module here:
http://docs.python.org/release/2.6/library/threading.html, and the
discussion on daemon vs non-daemon threads here:
http://blog.doughellmann.com/2008/01/pymotw-threading_13.html

Basically, as long as at least one non-daemon thread is still running,
your program will not exit.

> <snip code>

--
regards,
kushal

Kushal Kumaran

unread,
Apr 21, 2010, 4:23:05 AM4/21/10
to kaiix, pytho...@python.org
On Wed, Apr 21, 2010 at 1:50 PM, Kushal Kumaran
<kushal.kum...@gmail.com> wrote:
> On Wed, Apr 21, 2010 at 1:38 PM, kaiix <kvn...@gmail.com> wrote:
>> A simple thread pool example. My question is, since *MyThread.run*
>> will loop endless, how does the thread know the time to quit? how does
>> the *queue* notify the thread? is there any shared variables, like a
>> *lock*?
>>
>> When I set daemon false, it stays in the loop, not quit any more.
>> what's the role does the daemon state plays?
>>
>
> Take a look at the documentation for the threading module here:
> http://docs.python.org/release/2.6/library/threading.html, and the
> discussion on daemon vs non-daemon threads here:
> http://blog.doughellmann.com/2008/01/pymotw-threading_13.html
>
> Basically, as long as at least one non-daemon thread is still running,
> your program will not exit.
>
>> <snip code>
>

And I should have read your mail better. Take a look at the
documentation of the task_done method:
http://docs.python.org/library/queue.html

--
regards,
kushal

kaiix

unread,
Apr 21, 2010, 5:36:12 AM4/21/10
to
@kushal, thanks for your replies.

before i wrote the email, i've already read the python docs carefully.
i need the proof from code, i mean python source code. i tried to
prove some of my assumptions that lead the loop quit, and i traced
back to Queue.py, threading.py, dummy_thread.py, now i need some hints
to help me understanding the sample from python source code.

James Mills

unread,
Apr 21, 2010, 6:00:42 AM4/21/10
to kaiix, pytho...@python.org

You would need to look into the source code of
python's builtin 'thread' module which is the low-level
threading implementation used by python's 'threading'
module (which is a wrapper atop this).

'threading' aside from everything else it does, really in the
end calls thread.start_new_thread(...)

cheers
James

Steve

unread,
Apr 23, 2010, 9:28:54 AM4/23/10
to

Here's a slightly different example that demonstrates more clearly how
to end each thread.

http://effbot.org/librarybook/queue.htm

0 new messages