Exceptions and locks

Kevin Jacobs jacobs at darwin.epbi.cwru.edu
Tue Apr 10 13:15:06 EDT 2001


Michael Str?der <michael at stroeder.com> wrote:
> I have to use threading.Lock() objects in my application.

> I'm currently doing something like this code below to make sure that
> locks are released also in case of any exception raised:

> my_lock = threading.Lock()

> my_lock.acquire()
> try:
>   ..some single action..
> except:
>   my_lock.release()
>   raise
> my_lock.release()

> But this looks crude to me. Any more elegant way of doing this?

my_lock = threading.Lock()
my_lock.acquire()

try:
  foo()
finally:
  my_lock.release()

-Kevin

-- 
----------->  Kevin Jacobs  <-----------|------->  (216) 986-0710  <--------
Informatics Consultant                  | Department of Epidemiology
Primary mail:   jacobs at theopalgroup.com |   & Biostatistics
Alternate mail: jacobs at darwin.cwru.edu  | Case Western Reserve University
----------------------------------------------------------------------------



More information about the Python-list mailing list