Synchronization methodology

Duncan Booth duncan.booth at invalid.invalid
Wed Jan 3 06:09:00 EST 2007


Marc 'BlackJack' Rintsch <bj_666 at gmx.net> wrote:

> You may want to make sure the lock will be released in case of an
> exception:
> 
> def foo(self):
>     self.lock.aquire()
>     try:
>         pass # code
>     finally:
>         self.lock.release()
> 

In Python 2.5 this can also be written more succintly using the with 
statement so foo becomes:

from __future__ import with_statement
...

    def foo(self):
        with self.lock:
            pass # insert your code here


or write a decorator.



More information about the Python-list mailing list