[Python-bugs-list] [ python-Bugs-456024 ] event synchronization

noreply@sourceforge.net noreply@sourceforge.net
Wed, 05 Sep 2001 11:27:18 -0700


Bugs item #456024, was opened at 2001-08-27 22:56
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=456024&group_id=5470

Category: Threads
Group: Feature Request
>Status: Closed
>Resolution: Rejected
Priority: 5
Submitted By: Nobody/Anonymous (nobody)
Assigned to: Nobody/Anonymous (nobody)
Summary: event synchronization

Initial Comment:
It would be nice if the set and clear operations
on Event objects atomically returned the old state of
the internal flag:

  old_state = event.set()
  if old_state: nevermind()
  else: do_stuff ()

sets the flag to true and returns true if the flag
was already set, otherwise returns false.

This avoids the obvious race condition in
  if event.isSet(): nevermind()
  else: 
     event.set()
     do_stuff()

Maybe there's a way to accomplish this with
Semaphore objects (I haven't figured this thread
stuff out yet) but the event interface is more
natural for some purposes (i.e. the event represents
a lock on a resource).


----------------------------------------------------------------------

>Comment By: Guido van Rossum (gvanrossum)
Date: 2001-09-05 11:27

Message:
Logged In: YES 
user_id=6380

Rejected, for lack of opposition.


----------------------------------------------------------------------

Comment By: Tim Peters (tim_one)
Date: 2001-08-28 19:56

Message:
Logged In: YES 
user_id=31435

I'm opposed to this.  Events are very feeble mechanisms, 
best reserved for dead-simple cases where an event is set 
once, is never cleared, and it doesn't matter if it's set 
multiple times (if you find yourself clearing an event, 
your code is likely wrong).  If you want a lock, 
threading.py already supplies two flavors:

if lock.acquire(0):
.   # we own the lock now
.   do_stuff()
.   lock.release()
else:
.   # the lock wasn't available
.   nevermind()

A non-blocking acquire can also be done with a Sempahore, 
but that's overkill unless the semaphore is counting a pool 
(more than one) of resources.

----------------------------------------------------------------------

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=456024&group_id=5470