Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #17501

Re: Make a small function thread safe

References (3 earlier) <CAF-oDbF2xXiBk6oK7gmA=vJQ7Lt3vDB3p9Ckq3nMNihgXt=fNQ@mail.gmail.com> <mailman.3754.1324073306.27778.python-list@python.org> <4eebfbff$0$1679$742ec2ed@news.sonic.net> <17238811.518.1324198329306.JavaMail.geo-discussion-forums@yqio4> <CAN8CLgm6LjCn4JB7+CytrfRo9r3bZD1evYWw8QwFp45O+AYYCg@mail.gmail.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date 2011-12-18 23:57 -0700
Subject Re: Make a small function thread safe
Newsgroups comp.lang.python
Message-ID <mailman.3816.1324277895.27778.python-list@python.org> (permalink)

Show all headers | View raw


On Sun, Dec 18, 2011 at 6:27 PM, Tim Delaney
<timothy.c.delaney@gmail.com> wrote:
> On 18 December 2011 19:52, RangerElf <gustavo.cordova@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.

Well, not unless you make the same mistake in the context manager itself.

from contextlib import contextmanager

@contextmanager
def bad_context_manager(lock):
    try:
        lock.acquire()
        yield
    finally:
        lock.release()

class Lock(object):
    def __init__(self):
        self.is_locked = False
    def acquire(self):
        assert False
    def release(self):
        assert self.is_locked, "Tried to release lock without acquiring it"

with bad_context_manager(Lock()):
    pass

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python2.7/contextlib.py", line 17, in __enter__
    return self.gen.next()
  File "<stdin>", line 7, in bad_context_manager
  File "<stdin>", line 7, in release
AssertionError: Tried to release lock without acquiring it


Perhaps a (small) reason to avoid the contextmanager decorator and
implement __enter__ and __exit__ instead.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Make a small function thread safe Brad Tilley <kj4eit@gmail.com> - 2011-12-16 05:21 -0800
  Re: Make a small function thread safe Tim Wintle <tim.wintle@teamrubber.com> - 2011-12-16 13:33 +0000
  Re: Make a small function thread safe Tim Wintle <tim.wintle@teamrubber.com> - 2011-12-16 14:36 +0000
    Re: Make a small function thread safe Brad Tilley <kj4eit@gmail.com> - 2011-12-16 07:05 -0800
  Re: Make a small function thread safe Lie Ryan <lie.1296@gmail.com> - 2011-12-17 09:08 +1100
    Re: Make a small function thread safe John Nagle <nagle@animats.com> - 2011-12-16 18:18 -0800
      Re: Make a small function thread safe RangerElf <gustavo.cordova@gmail.com> - 2011-12-18 00:52 -0800
        Re: Make a small function thread safe Ian Kelly <ian.g.kelly@gmail.com> - 2011-12-18 23:57 -0700
  Re: Make a small function thread safe ting@thsu.org - 2011-12-19 15:56 -0800

csiph-web