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

clear() should return prior state in threading.Event #73635

Closed
jmoy mannequin opened this issue Feb 5, 2017 · 6 comments
Closed

clear() should return prior state in threading.Event #73635

jmoy mannequin opened this issue Feb 5, 2017 · 6 comments
Labels
3.7 (EOL) end of life stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@jmoy
Copy link
Mannequin

jmoy mannequin commented Feb 5, 2017

BPO 29449
Nosy @tim-one, @rhettinger, @jmoy, @serhiy-storchaka

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 = None
closed_at = <Date 2017-02-09.16:24:10.809>
created_at = <Date 2017-02-05.07:37:48.044>
labels = ['3.7', 'invalid', 'type-feature', 'library']
title = 'clear() should return prior state in threading.Event'
updated_at = <Date 2017-02-09.16:24:10.808>
user = 'https://github.com/jmoy'

bugs.python.org fields:

activity = <Date 2017-02-09.16:24:10.808>
actor = 'jmoy'
assignee = 'none'
closed = True
closed_date = <Date 2017-02-09.16:24:10.809>
closer = 'jmoy'
components = ['Library (Lib)']
creation = <Date 2017-02-05.07:37:48.044>
creator = 'jmoy'
dependencies = []
files = []
hgrepos = []
issue_num = 29449
keywords = []
message_count = 6.0
messages = ['287039', '287097', '287108', '287109', '287275', '287435']
nosy_count = 4.0
nosy_names = ['tim.peters', 'rhettinger', 'jmoy', 'serhiy.storchaka']
pr_nums = []
priority = 'low'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue29449'
versions = ['Python 3.7']

@jmoy
Copy link
Mannequin Author

jmoy mannequin commented Feb 5, 2017

The clear() method for Event objects currently does not return anything.

It would be useful to have it return the state of the Event prior to it being cleared.

This would be useful since multiple threads may wake up from an event at the same time and all may try to clear() the event. A return value from clear will indicate to a thread if it won the race to clear the event.

@jmoy jmoy mannequin added 3.7 (EOL) end of life stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels Feb 5, 2017
@rhettinger
Copy link
Contributor

A return value from clear will indicate to a thread if it
won the race to clear the event.

Why would we care who won the race to clear? I would think that the important thing is that the event is cleared, not who did it.

@jmoy
Copy link
Mannequin Author

jmoy mannequin commented Feb 6, 2017

> A return value from clear will indicate to a thread if it
> won the race to clear the event.

Why would we care who won the race to clear? I would think that the
important thing is that the event is cleared, not who did it.

Here's the scenario that prompted my report: the Event is set to indicate
that certain 'work' has accumulated and one among a pool of workers uses
clear() to claim the work accumulated till that point. If clear() returned
a value, we could easily ensure that only one among the workers woken up
actually does the work.

Of course, in this case it would be more efficient to wake up just one
worker using a Condition object and notify() but then one has to write the
logic to maintain the state of the event. An Event whose clear() returned a
value would allow for a quick and dirty solution.

@serhiy-storchaka
Copy link
Member

A return value from clear will indicate to a thread if it won the race to clear the event.

I think other synchronization primitives should be used for this. I'm not sure that other implementations of Event (e.g. multiprocessing.Event) can efficiently return the previous state.

@tim-one
Copy link
Member

tim-one commented Feb 8, 2017

I can't judge a use case for a thread gimmick in the absence of wholly specified examples. There are too many possible subtleties. Indeed, if I'd do anything with Event.clear() it would be to remove it - I've seen too much code that suffers subtle race bugs when trying to reset an event. The one thing it's clearly good for is announcing a one-time global state change, for which it's sufficient, efficient, clear, and hard to get wrong.

Can you use a Barrier instead? The Barrier design allows reuse with no special care, Barrier.wait() already returns a little integer unique to each thread, and the Barrier constructor even allows a function to be passed in to be executed by (exactly) one of the threads when the Barrier is complete.

@jmoy
Copy link
Mannequin Author

jmoy mannequin commented Feb 9, 2017

Thanks for the comments above. I'm going to rethink my design. Closing this bug.

@jmoy jmoy mannequin closed this as completed Feb 9, 2017
@jmoy jmoy mannequin added the invalid label Feb 9, 2017
@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
3.7 (EOL) end of life stdlib Python modules in the Lib dir type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

3 participants