Visitar URL original
bpo-1360: Add warning to Queue.get() docs about block=True by sirosen · Pull Request #2223 · python/cpython · GitHub
The Wayback Machine - https://web.archive.org/web/20221223161252/https://github.com/python/cpython/pull/2223
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

bpo-1360: Add warning to Queue.get() docs about block=True #2223

Merged
merged 1 commit into from Mar 25, 2019

Conversation

sirosen
Copy link
Contributor

@sirosen sirosen commented Jun 15, 2017 •

When Queue.get() is called with block=True and timeout=None,
the thread will block and wait on the Queue's not_empty condition
variable without a timeout. Under these conditions, per the discussion
in https://bugs.python.org/issue1360 , if the main thread is waiting on
the get(), it will not respond to SIGINT.

This can be a source of confusion in scripts which want to use Queues to
wait on async processes, as Ctrl+C is commonplace.
As noted in issue1360, this is unlikely to change because it would slow down
lock acquisition, so update the docs to include a warning instead.

I don't think there are other particularly interesting scenarios which
deserve explicit mention in the docs.
Needs to be backported to 2.7, and all 3.x maintenance branches.

https://bugs.python.org/issue1360

@the-knights-who-say-ni
Copy link

the-knights-who-say-ni commented Jun 15, 2017

Hello, and thanks for your contribution!

I'm a bot set up to make sure that the project can legally accept your contribution by verifying you have signed the PSF contributor agreement (CLA).

Unfortunately we couldn't find an account corresponding to your GitHub username on bugs.python.org (b.p.o) to verify you have signed the CLA (this might be simply due to a missing "GitHub Name" entry in your b.p.o account settings). This is necessary for legal reasons before we can look at your contribution. Please follow the steps outlined in the CPython devguide to rectify this issue.

Thanks again to your contribution and we look forward to looking at it!

@sirosen sirosen changed the title Add warning to Queue.get() docs about block=True bpo-1360: Add warning to Queue.get() docs about block=True Jun 15, 2017
@sirosen
Copy link
Contributor Author

sirosen commented Jun 15, 2017

Just signed the CLA today. It will presumably take in the next couple of business days.

@eryksun
Copy link
Contributor

eryksun commented Jun 15, 2017

Queue.get can be interrupted in Python 3, at least on Linux. I think that's the case for all POSIX systems, and only Windows doesn't implement interrupting this wait via Ctrl+C. The shortcoming on Windows could be addressed in at least two ways with the existing condition-variable implementation based on critical sections and semaphores. On the other hand, as far as I can tell, the implementation based on slim reader/writer locks cannot be interrupted. There's significant interest to enable the latter as the default for the sake of performance, in which case it needs to be documented that get() will never be interruptible on Windows.

@sirosen
Copy link
Contributor Author

sirosen commented Jun 15, 2017

Queue.get can be interrupted in Python 3, at least on Linux.

I failed to check for this, sorry. It does appear to work, as you say.

Should I make changes to this PR to apply it (at least for now) to 2.x maintenance only? What is the procedure for doing this?


From my perspective, as a consumer of the queue lib not a maintainer, it's totally fine for get() to be uninterruptible as long as it's documented.
The problem today is that there's critical information in the referenced bpo issue which is not in the docs.

For Windows, rather than hypothesize about the future of this operation, I'd like to document the current behavior. I don't have a Windows box ready-to-hand for testing, but it sounds like you're saying Queue.get can't be interrupted on Windows, even under py3?

@eryksun
Copy link
Contributor

eryksun commented Jun 15, 2017

Yes, for Python 3 specifically on Windows, this PR can document that Queue.get uses an uninterruptible wait. For a wait with a timeout, the SIGINT handler runs after the wait times out.

@Mariatta Mariatta added the docs Documentation in the Doc dir label Jun 16, 2017
@sirosen
Copy link
Contributor Author

sirosen commented Jun 16, 2017

I've updated the warning to note the exact versions and platforms on which this is a problem.

I would have liked to make the 2.x warning different from the 3.x warning, but I'm not sure what the dev workflow for that would be. Plus it makes backporting future changes to the doc harder.

an uninterruptible wait on an underlying lock.

This means that no exceptions can occur, and in particular a SIGINT will
not trigger a ``KeyboardInterrupt``.
Copy link
Member

@matrixise matrixise Nov 4, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when you do a reference to an exception, you can use the :exc: role like that :exc:`KeyboardInterrupt`

@sirosen sirosen changed the base branch from master to 2.7 Mar 15, 2019
@sirosen sirosen changed the base branch from 2.7 to master Mar 15, 2019
@sirosen sirosen requested a review from rhettinger as a code owner Mar 15, 2019
@sirosen
Copy link
Contributor Author

sirosen commented Mar 15, 2019 •

For a moment, I was going to retarget to py2.7, but looking more closely again, it's a valid fix for py2 and py3 docs.

I've added the :exc:, but I'm happy to make other changes too -- rebase and squash, break it up into separate doc changes for py2 vs py3, etc. Whatever works best for the cpython devs.

@@ -128,6 +128,15 @@ provide the public methods described below.
Otherwise (*block* is false), return an item if one is immediately available,
else raise the :exc:`Empty` exception (*timeout* is ignored in that case).

.. warning::
Copy link
Contributor

@rhettinger rhettinger Mar 20, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's just add this as plain text. The warning directive is typically saved for cases that are several or common. Otherwise, it creates visual litter than makes the docs hard to read (the eye is first drawn to the warning boxes).

@bedevere-bot
Copy link

bedevere-bot commented Mar 20, 2019

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

And if you don't make the requested changes, you will be put in the comfy chair!

@sirosen
Copy link
Contributor Author

sirosen commented Mar 20, 2019

Removed the warning -- reasoning makes perfect sense. I also rebased to squash + cleanup the commit messages a bit. Per Sir Bedevere's instructions...

I have made the requested changes; please review again.

@bedevere-bot
Copy link

bedevere-bot commented Mar 20, 2019

Thanks for making the requested changes!

@rhettinger: please review the changes made to this pull request.

@@ -151,6 +151,14 @@ provide the public methods described below.
else raise the :exc:`Empty` exception (*timeout* is ignored in that case).

Copy link
Contributor

@rhettinger rhettinger Mar 20, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Remove this blank line.

Prior to 3.0 on POSIX systems, and for all versions on Windows, if
*block* is true and *timeout* is ``None``, this operation goes into
an uninterruptible wait on an underlying lock.

Copy link
Contributor

@rhettinger rhettinger Mar 20, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Join these two sentences into a single paragraph. The second sentence depends on the first and is the crux of the point being made.

@bedevere-bot
Copy link

bedevere-bot commented Mar 20, 2019

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@rhettinger
Copy link
Contributor

rhettinger commented Mar 20, 2019

Python 2.7 is nearing end-of-life, so we're now mostly only backporting changes that are relatively important.

When Queue.get() is called with `block=True` and `timeout=None`,
the thread will block and wait on the Queue's not_empty condition
variable without a timeout. Under these conditions, per the discussion
in https://bugs.python.org/issue1360 , if the main thread is waiting on
the get(), it will not respond to SIGINT.

This can be a source of confusion in scripts which want to use Queues to
wait on async processes, as Ctrl+C is commonplace.
As noted in issue1360, this is unlikely to change because it would slow down
lock acquisition, so update the docs to include a warning instead.

As of 3.0, the uninterruptible nature of Queue.get() has changed.
However, the problem remains on all versions for Windows. There doesn't
seem to be prescriptive guidance for how to handle these kinds
of version-specific warnings in any of the contributing guides. Simply
describing the version/platform constraints seems fine.
@sirosen
Copy link
Contributor Author

sirosen commented Mar 22, 2019

I've amended with the copy-edit changes, and in case I haven't said above or forget to say it later: thanks for all of the review and attention!

Python 2.7 is nearing end-of-life, so we're now mostly only backporting changes that are relatively important.

Fair/understood. I agree that this isn't a very important change.


I have made the requested changes; please review again.

@bedevere-bot
Copy link

bedevere-bot commented Mar 22, 2019

Thanks for making the requested changes!

@rhettinger: please review the changes made to this pull request.

@rhettinger rhettinger merged commit 713a8ae into python:master Mar 25, 2019
@miss-islington
Copy link
Contributor

miss-islington commented Mar 25, 2019

Thanks @sirosen for the PR, and @rhettinger for merging it 🌮🎉.. I'm working now to backport this PR to: 3.7.
🐍🍒⛏🤖

@bedevere-bot
Copy link

bedevere-bot commented Mar 25, 2019

GH-12538 is a backport of this pull request to the 3.7 branch.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Mar 25, 2019
(cherry picked from commit 713a8ae)

Co-authored-by: Stephen Rosen <sirosen@globus.org>
rhettinger pushed a commit that referenced this pull request Mar 25, 2019
(cherry picked from commit 713a8ae)

Co-authored-by: Stephen Rosen <sirosen@globus.org>
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 skip news
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants