Issue 40387: queue example is a sketch - Python tracker

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