[New-bugs-announce] [issue12155] queue example doesn't stop worker threads

STINNER Victor report at bugs.python.org
Mon May 23 12:22:31 CEST 2011


New submission from STINNER Victor <victor.stinner at haypocalc.com>:

The queue doc contains the following example:
------------------
def worker():
    while True:
        item = q.get()
        do_work(item)
        q.task_done()

q = Queue()
for i in range(num_worker_threads):
     t = Thread(target=worker)
     t.daemon = True
     t.start()

for item in source():
    q.put(item)

q.join()       # block until all tasks are done
------------------
http://docs.python.org/library/queue.html

It doesn't define do_work(), num_worker_threads or do_work(), but my concern is that it doesn't stop worker threads.

I consider "t.daemon = True" as an hack to not care about stopping threads.

The example should pass a special value to each worker to stop it. For example:

<worker>

while True:
  job = queue.get()
  if job is None:
     break
  audio.play(*job)
  queue.task_done()

Main thread:

...
threads = []
for i in range(num_worker_threads):
     t = Thread(target=worker)
     threads.append(t)
     t.start()
...
for i in range(num_worker_threads):
     queue.put(None)
queue.join()
for thread in threads:
    thread.join()

----------
assignee: docs at python
components: Documentation
messages: 136601
nosy: docs at python, haypo, pitrou
priority: normal
severity: normal
status: open
title: queue example doesn't stop worker threads
versions: Python 2.7, Python 3.1, Python 3.2, Python 3.3

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue12155>
_______________________________________


More information about the New-bugs-announce mailing list