This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: queue example is a sketch
Type: enhancement Stage: resolved
Components: Documentation Versions: Python 3.9, Python 3.8
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: Santiago Blanco, docs@python, miss-islington, python-dev, remi.lapeyre, rhettinger
Priority: normal Keywords: patch

Created on 2020-04-25 07:58 by Santiago Blanco, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
example.py Santiago Blanco, 2020-04-25 07:58 A working example
Pull Requests
URL Status Linked Edit
PR 19713 closed python-dev, 2020-04-25 14:48
PR 19724 merged rhettinger, 2020-04-26 23:53
PR 19726 merged miss-islington, 2020-04-27 01:11
Messages (8)
msg367263 - (view) Author: Santiago Blanco (Santiago Blanco) * Date: 2020-04-25 07:58
Copy the example of the next URL:

https://docs.python.org/3/library/queue.html#queue.Queue.join

and paste into a file, then try to run it. It does not work.

I have tried a new one (attachment) that works.
msg367274 - (view) Author: Rémi Lapeyre (remi.lapeyre) * Date: 2020-04-25 11:35
In general examples in the documentation may lack some things like imports as they are implicit but in this case it also lack do_work(), source() and num_worker_threads.

You could use os.cpu_count() instead of your hard-coded value and the comment.

Can you open a pull request with your change?
msg367279 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2020-04-25 18:27
The example is supposed to be a sketch of the overall pattern.  IMO, putting concrete and useless implementations of do_work() and source() make the example less intelligible.

Instead, we can add a note that the user needs to supply their own do_work() and source().
msg367323 - (view) Author: Santiago Blanco (Santiago Blanco) * Date: 2020-04-26 17:57
I am trying to help, if you want I change something of my PR tell me what.

I think most people need concrete examples to learn anything, but I may be wrong.

I consider that a note will not change anything.
msg367333 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2020-04-26 23:29
I'm thinking of simplifying the example to focus on its core goal of demonstrating how to enqueue tasks and then wait for them to be finished:

    import threading, queue

    q = queue.Queue()

    def worker():
        while True:
            item = q.get()
            print(f'Working on {item}')
            print(f'Finished {item}')
            q.task_done()

    # turn-on the worker thread
    worker_thread = threading.Thread(target=worker)
    worker_thread.daemon = True
    worker_thread.start()

    # send thirty task requests to the worker
    for item in range(30):
        q.put(item)
    print('All task requests sent\n', end='')    

    # block until all tasks are done
    q.join()
    print('All work completed')
msg367336 - (view) Author: Santiago Blanco (Santiago Blanco) * Date: 2020-04-27 00:26
That is pretty clear! Thank you Raymond.
msg367338 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2020-04-27 01:11
New changeset 88499f15f547ccf7b15d37b0eaf51cc40bad5c39 by Raymond Hettinger in branch 'master':
bpo-40387: Improve queue join() example. (GH-19724)
https://github.com/python/cpython/commit/88499f15f547ccf7b15d37b0eaf51cc40bad5c39
msg367339 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2020-04-27 01:23
New changeset 179f22c3b786ce9baa3445923f8f9708dfa5d5b7 by Miss Islington (bot) in branch '3.8':
bpo-40387: Improve queue join() example. (GH-19724) (GH-19726)
https://github.com/python/cpython/commit/179f22c3b786ce9baa3445923f8f9708dfa5d5b7
History
Date User Action Args
2022-04-11 14:59:29adminsetgithub: 84567
2020-04-27 01:24:14rhettingersetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2020-04-27 01:23:21rhettingersetmessages: + msg367339
2020-04-27 01:11:57miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request19048
2020-04-27 01:11:34rhettingersetmessages: + msg367338
2020-04-27 00:26:48Santiago Blancosetmessages: + msg367336
2020-04-26 23:53:25rhettingersetpull_requests: + pull_request19046
2020-04-26 23:29:34rhettingersetassignee: docs@python -> rhettinger
title: queue example it does not work -> queue example is a sketch
messages: + msg367333
versions: - Python 3.7
2020-04-26 17:57:40Santiago Blancosetmessages: + msg367323
2020-04-25 18:27:08rhettingersetnosy: + rhettinger
messages: + msg367279
2020-04-25 14:48:40python-devsetkeywords: + patch
nosy: + python-dev

pull_requests: + pull_request19035
stage: patch review
2020-04-25 11:35:44remi.lapeyresetnosy: + remi.lapeyre
messages: + msg367274
2020-04-25 07:58:55Santiago Blancocreate