Python complaints

Toby J Sargeant tjs at longford.cs.monash.edu.au
Thu Nov 25 04:10:28 CET 1999


On Fri, Nov 26, 1999 at 12:16:50AM +1300, Greg Ewing wrote:
> Tim Peters wrote:
> > 
> >     mutex.acquire()
> >         do stuff
> >     mutex.release()
> > 
> > is a syntax error in Python.
> 
> What's *really* needed here is a way to abstract these kinds
> of constructs. In Scheme I would arrange things so that I
> could write
> 
>    (with-mutex-acquired mutex
>       some 
>       stuff)
> 
> or in Smalltalk
> 
>    mutex do: [
>       some
>       stuff
>    ]

Just throwing this up in the air, and seeing what comes back...

class Acquire:
   def __init__(self,lock):
      self.lock=lock
      lock.acquire()

   def __del__(self):
      self.lock.release()

def function(mutex):
  _a=Acquire(mutex)
  # do stuff.

It relies on Acquire.__del__() being called as soon as _a goes out
of scope, but that doesn't seem likely to change for the moment.
(Of course doing this in JPython is a recipe for disaster... or
at least extreme inefficiency)

It also has the advantage that you can exit the function in many
ways (including by exception), and the lock will be released
regardless.

It also makes the whole function a critical section which is not
always what's wanted.

thoughts?

Toby.




More information about the Python-list mailing list