Locking and try/finally

Duncan Grisby duncan-news at grisby.org
Wed Dec 4 05:40:57 EST 2002


In article <d111eafa.0212032054.124c706e at posting.google.com>,
xtian <xtian at toysinabag.com> wrote:

>	    def critical_function(self):
>		self._mutex.acquire()
>		try:
>		    # Manipulate data shared by threads
>		finally:
>		    self._mutex.release()

One way to make this kind of thing cleaner is to make locking part of
the language syntax. Modula-3, one of the inspirations of Python, did
things like this... (the syntax is probably wrong, but you get the
idea)

  PROCEDURE critical_function(self: T) =
    BEGIN
      LOCK self.mu DO
        (* Manipulate data... *)
      END;
    END;

The compiler turned such statements into the equivalent acquire /
release pair using TRY..FINALLY.

I think it would be great if Python had the same kind of statement,
but I suppose it's unlikely to happen.

Cheers,

Duncan.

-- 
 -- Duncan Grisby         --
  -- duncan at grisby.org     --
   -- http://www.grisby.org --



More information about the Python-list mailing list