Python complaints

Greg Ewing greg.ewing at compaq.com
Thu Nov 25 12:16:50 CET 1999


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
   ]

In Python you have to either put the stuff in a separate
function (which interrupts the flow of thought for both the
writer and reader) or use a lambda (which severely restricts
the form of the stuff). In either case you have scope problems.

So what I'd like is a construct something like

   with_mutex_acquired(mutex):
      some
      stuff

which would call a function defined like

   def with_mutex_acquired(thunk, mutex):
      mutex.acquire()
      try:
         thunk()
      finally:
         mutex.release()

Greg




More information about the Python-list mailing list