Make a small function thread safe

Tim Delaney timothy.c.delaney at gmail.com
Sun Dec 18 20:27:24 EST 2011


On 18 December 2011 19:52, RangerElf <gustavo.cordova at gmail.com> wrote:

> Which is why the original .acquire() ... .release() idiom was wrong, this
> would better express the intent:
>
> try:
>  lock.acquire()
>  shared_container.append(...)
> finally:
>  lock.release()
>

No - this is very bad. The lock must be acquired outside the try: -
otherwise if an exception is thrown while acquiring, you will try to
release a lock that you have not acquired.

Which again is why using with is a much better option - you can't make this
kind of mistake.

Tim Delaney
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20111219/3ecc9472/attachment-0001.html>


More information about the Python-list mailing list