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

Make threading.Event().wait(timeout=3) return isSet #44660

Closed
davidfraser mannequin opened this issue Mar 5, 2007 · 9 comments
Closed

Make threading.Event().wait(timeout=3) return isSet #44660

davidfraser mannequin opened this issue Mar 5, 2007 · 9 comments
Assignees
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@davidfraser
Copy link
Mannequin

davidfraser mannequin commented Mar 5, 2007

BPO 1674032
Nosy @gvanrossum, @tim-one, @birkenfeld, @pitrou
Files
  • event-wait-retval.diff: proposed patch for threading.py and documentation
  • 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/birkenfeld'
    closed_at = <Date 2009-03-31.20:41:16.373>
    created_at = <Date 2007-03-05.13:33:33.000>
    labels = ['type-feature', 'library']
    title = 'Make threading.Event().wait(timeout=3) return isSet'
    updated_at = <Date 2009-03-31.20:41:16.372>
    user = 'https://bugs.python.org/davidfraser'

    bugs.python.org fields:

    activity = <Date 2009-03-31.20:41:16.372>
    actor = 'georg.brandl'
    assignee = 'georg.brandl'
    closed = True
    closed_date = <Date 2009-03-31.20:41:16.373>
    closer = 'georg.brandl'
    components = ['Library (Lib)']
    creation = <Date 2007-03-05.13:33:33.000>
    creator = 'davidfraser'
    dependencies = []
    files = ['10203']
    hgrepos = []
    issue_num = 1674032
    keywords = ['patch']
    message_count = 9.0
    messages = ['55044', '66317', '66324', '71566', '72697', '83556', '84546', '84569', '84895']
    nosy_count = 7.0
    nosy_names = ['gvanrossum', 'tim.peters', 'georg.brandl', 'davidfraser', 'pitrou', 'tlesher', 'carmiac']
    pr_nums = []
    priority = 'normal'
    resolution = 'accepted'
    stage = None
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue1674032'
    versions = ['Python 3.1', 'Python 2.7']

    @davidfraser
    Copy link
    Mannequin Author

    davidfraser mannequin commented Mar 5, 2007

    Currently the wait method on threading.Event always returns None, even if a timeout is given and the event is not set.
    This means that there is no way to determine whether the wait method returned because the event was set, or because the timeout period expired, without querying the event status again:

    x.wait(3)
    if x.isSet():
      # do stuff

    Note that in the above case, the event could be cleared between the return from x.wait and the execution of x.isSet (in another thread), and so this would operate as though x.wait had just timed out

    It would be great to be able to do:

    if x.wait(3):
      # do stuff

    This should also not affect any existing code as it shouldn't be relying on the return value from x.wait anyway

    @davidfraser davidfraser mannequin added interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement labels Mar 5, 2007
    @tlesher
    Copy link
    Mannequin

    tlesher mannequin commented May 6, 2008

    This one-line change to threading.py makes Event.wait() return isSet().
    Also includes the corresponding update to documentation in threading.rst.

    @tlesher tlesher mannequin added docs Documentation in the Doc dir stdlib Python modules in the Lib dir and removed interpreter-core (Objects, Python, Grammar, and Parser dirs) labels May 6, 2008
    @tlesher tlesher mannequin assigned birkenfeld May 6, 2008
    @birkenfeld
    Copy link
    Member

    Tim, you said this was a bad idea for conditions in bpo-1175933 - is the
    same true for Events?

    @birkenfeld birkenfeld assigned tim-one and unassigned birkenfeld May 6, 2008
    @carmiac
    Copy link
    Mannequin

    carmiac mannequin commented Aug 20, 2008

    I would like to add my support for this change. As David described, it
    is often useful to know why the x.wait() has returned, not just that it has.

    @akuchling akuchling removed the docs Documentation in the Doc dir label Sep 6, 2008
    @pitrou
    Copy link
    Member

    pitrou commented Sep 6, 2008

    I agree this would be a worthwhile addition too. Unfortunately given the
    current schedule this will probably have to wait for 2.7/3.1.

    @pitrou
    Copy link
    Member

    pitrou commented Mar 14, 2009

    A test should be added to the patch, and then I think it could go in.

    @tlesher
    Copy link
    Mannequin

    tlesher mannequin commented Mar 30, 2009

    Thanks, Antoine. I will re-check the patch against trunk and add tests
    this week.

    @gvanrossum
    Copy link
    Member

    Looking at this, I think this change is fine. The _Event class itself
    holds the condition that it's checking for, and the is_set() method
    doesn't acquire the lock, so there's no reason to prefer

    e.wait()
    if e.is_set():
        GOT_IT()

    over

    if e.wait():
        GOT_IT()

    IOW Tim's reasoning in bpo-1175933 for rejecting a similar change to
    _Condition.wait() doesn't apply here. I think we can go ahead without
    waiting for Tim to confirm this.

    @birkenfeld
    Copy link
    Member

    Added a pony test and committed in r70883.

    @birkenfeld birkenfeld assigned birkenfeld and unassigned tim-one Mar 31, 2009
    @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
    stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    5 participants