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) *  |
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) *  |
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) *  |
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) *  |
Date: 2010-04-21 16:27 |
This is a duplicate of #5998, which was rejected. |
|
|