Locking and try/finally
Geoffrey Talvola
gtalvola at nameconnector.com
Wed Dec 4 12:03:03 EST 2002
xtian at toysinabag.com [mailto:xtian at toysinabag.com] wrote:
...
> Obtaining locks is simpler in C++ than in Python. The equivalent
> Python code is always 3 lines longer. In fact, about 9% of the Python
> implementation of Recall is simply obtaining and releasing locks
> safely. Yet, overall, the Python code is smaller.
...
Instead of writing:
mutex.acquire()
try:
# do something
finally:
mutex.release()
How about wrapping up the try/finally and acquire/release into a method of
the mutex object that takes a callable as its argument:
def task():
# do something
mutex.call(task)
This saves 2 lines, as long as the "do something" part can be placed into a
nested function (which isn't always possible, but often is, using nested
scopes).
- Geoff
More information about the Python-list
mailing list