Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

queue example doesn't stop worker threads #56364

Closed
vstinner opened this issue May 23, 2011 · 5 comments
Closed

queue example doesn't stop worker threads #56364

vstinner opened this issue May 23, 2011 · 5 comments
Assignees
Labels
docs Documentation in the Doc dir

Comments

@vstinner
Copy link
Member

BPO 12155
Nosy @rhettinger, @terryjreedy, @pitrou, @vstinner

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = 'https://github.com/rhettinger'
closed_at = <Date 2015-03-18.13:06:34.613>
created_at = <Date 2011-05-23.10:22:30.620>
labels = ['docs']
title = "queue example doesn't stop worker threads"
updated_at = <Date 2015-03-18.13:06:34.612>
user = 'https://github.com/vstinner'

bugs.python.org fields:

activity = <Date 2015-03-18.13:06:34.612>
actor = 'vstinner'
assignee = 'rhettinger'
closed = True
closed_date = <Date 2015-03-18.13:06:34.613>
closer = 'vstinner'
components = ['Documentation']
creation = <Date 2011-05-23.10:22:30.620>
creator = 'vstinner'
dependencies = []
files = []
hgrepos = []
issue_num = 12155
keywords = []
message_count = 5.0
messages = ['136601', '136710', '136724', '137153', '238426']
nosy_count = 6.0
nosy_names = ['rhettinger', 'terry.reedy', 'pitrou', 'vstinner', 'docs@python', 'python-dev']
pr_nums = []
priority = 'low'
resolution = 'fixed'
stage = None
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue12155'
versions = ['Python 3.5']

@vstinner
Copy link
Member Author

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

@vstinner vstinner added the docs Documentation in the Doc dir label May 23, 2011
@rhettinger rhettinger assigned rhettinger and unassigned docspython May 24, 2011
@rhettinger
Copy link
Contributor

It doesn't define do_work(), num_worker_threads or do_work()

Is it unclear to you what those mean? They are placeholders for the user's actual task at hand.

my concern is that it doesn't stop worker threads.

Stopping the threads wasn't the intent of the example.

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

If you post a high-quality self-contained example somewhere on the net, I would be happy to link to it.

@vstinner
Copy link
Member Author

Is it unclear to you what those mean?

Well, it's clear, but I like when I can simply copy/paste the example and it does just work:

If you post a high-quality self-contained example somewhere
on the net, I would be happy to link to it.

I just propose to change the example to stop the threads:
------------------

def worker():
    while True:
        item = q.get()
        if item is None:
            break
        do_work(item)
        q.task_done()

q = Queue()
threads = []
for i in range(num_worker_threads):
     t = Thread(target=worker)
     threads.append(t)
     t.start()

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

q.join()       # block until all tasks are done
for i in range(num_worker_threads):
    q.put(None)
for t in threads: t.join()

@terryjreedy
Copy link
Member

The proposed change adds about 7 lines to show the 'trick' of putting num-worker Nones on the queue. I think that is worth it.

@python-dev
Copy link
Mannequin

python-dev mannequin commented Mar 18, 2015

New changeset b44ec269abda by Victor Stinner in branch 'default':
Issue bpo-12155: Fix queue doc example to join threads
https://hg.python.org/cpython/rev/b44ec269abda

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir
Projects
None yet
Development

No branches or pull requests

3 participants