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
Title: threading.Event()
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.1, Python 3.2, Python 3.3, Python 2.7
process
Status: closed Resolution: rejected
Dependencies: Superseder: Add __bool__ to threading.Event and multiprocessing.Event
View: 5998
Assigned To: Nosy List: Kain94, pitrou, r.david.murray
Priority: normal Keywords:

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:00adminsetgithub: 52732
2010-04-21 16:27:51r.david.murraysetstatus: open -> closed
priority: normal
superseder: Add __bool__ to threading.Event and multiprocessing.Event


nosy: + r.david.murray
messages: + msg103863
resolution: rejected
stage: resolved
2010-04-21 14:07:23pitrousetmessages: + msg103847
title: [PATCH] threading.Event() -> threading.Event()
2010-04-21 13:52:44Kain94setmessages: + msg103844
2010-04-21 13:27:25pitrousetmessages: + msg103842
2010-04-21 13:18:27Kain94setmessages: + msg103841
2010-04-21 12:57:47pitrousetnosy: + pitrou
messages: + msg103837
2010-04-21 12:49:27Kain94create