K
kaiix
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()
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()