Issue 8486: threading.Event() - Python tracker (original) (raw)

Created on 2010-04-21 12:49 by Kain94, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (7)
msg103835 - (view) Author: Benjamin VENELLE (Kain94) Date: 2010-04-21 12:49
Hi, I'm suggesting to declare a __bool__() function as an alias for is_set() in Event class from threading package. So Event objects could be used like booleans. Ex: I thought 'while(not event): do_something()' is much intuitive than 'while(not event.is_set()): do_something()'
msg103837 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-04-21 12:57
Due to the nature of an Event, you should in most situations wait() on it, rather than test its value. Also, changing the boolean value of an Event would break existing code. I therefore think this should be rejected.
msg103841 - (view) Author: Benjamin VENELLE (Kain94) Date: 2010-04-21 13:18
Yes, I agree when Event objects are used to sync threads. But in my case, I'm using them like signals to terminate some server's thread through a proper way. This is a just a "cosmetic" stuff to enhance readability.
msg103842 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-04-21 13:27
> Yes, I agree when Event objects are used to sync threads. But in my > case, I'm using them like signals to terminate some server's thread > through a proper way. In this case, you don't have to use an Event. A boolean attribute on one of your objects is enough. (similarly, you would use a "volatile" variable in C)
msg103844 - (view) Author: Benjamin VENELLE (Kain94) Date: 2010-04-21 13:52
> In this case, you don't have to use an Event. A boolean attribute on one > of your objects is enough. > (similarly, you would use a "volatile" variable in C) This is already the case with Event. Look at threading.py @line ~355 Event class has a kinda private attribute _flags which is set/unset right before notifying. Using a boolean would look almostly the same so why to reinvent the wheel ? :) If you look also at is_set(), it has the same meanings than __bool__() (ie returning True or False according to object's status). My suggest is only to had the following two lines: def __bool__(self): return self.is_set()
msg103847 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-04-21 14:07
> Using a boolean would look almostly the same so why to reinvent the > wheel ? :) Hmm, /you/ are reinventing the wheel by suggesting to use an Event instead of a simple boolean. Just loop like this: while not self.stop_me: # etc. and set the stop_me attribute to True when you want to stop your thread.
msg103863 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2010-04-21 16:27
This is a duplicate of #5998, which was rejected.
History
Date User Action Args
2022-04-11 14:57:00 admin set github: 52732
2010-04-21 16:27:51 r.david.murray set status: open -> closedpriority: normalsuperseder: Add __bool__ to threading.Event and multiprocessing.Eventnosy: + r.david.murraymessages: + resolution: rejectedstage: resolved
2010-04-21 14:07:23 pitrou set messages: + title: [PATCH] threading.Event() -> threading.Event()
2010-04-21 13:52:44 Kain94 set messages: +
2010-04-21 13:27:25 pitrou set messages: +
2010-04-21 13🔞27 Kain94 set messages: +
2010-04-21 12:57:47 pitrou set nosy: + pitroumessages: +
2010-04-21 12:49:27 Kain94 create