Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!194.109.133.84.MISMATCH!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.008 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'context': 0.04; 'assert': 0.09; 'decorator': 0.09; 'finally:': 0.09; 'thrown': 0.09; 'wrong,': 0.09; 'exception': 0.12; 'def': 0.13; '(small)': 0.16; 'acquired.': 0.16; 'contextlib': 0.16; 'mistake.': 0.16; 'subject:function': 0.16; 'wrote:': 0.18; 'acquired': 0.18; '(most': 0.21; 'dec': 0.22; 'header:In-Reply-To:1': 0.22; 'traceback': 0.24; 'import': 0.27; 'tried': 0.27; 'skip:" 30': 0.28; 'message-id:@mail.gmail.com': 0.28; 'pass': 0.29; 'yield': 0.29; 'skip:b 20': 0.29; 'pm,': 0.29; 'class': 0.29; 'lock': 0.30; 'sun,': 0.30; 'implement': 0.32; "can't": 0.32; 'to:addr:python- list': 0.34; 'received:209.85.212': 0.34; '17,': 0.34; '8bit%:3': 0.34; 'last):': 0.34; 'skip:@ 10': 0.34; 'try:': 0.34; 'unless': 0.35; 'file': 0.36; '...': 0.36; 'instead.': 0.37; 'received:google.com': 0.37; 'skip:_ 10': 0.37; 'using': 0.38; 'received:209.85': 0.38; 'itself.': 0.39; 'skip:\xa0 10': 0.39; 'option': 0.39; 'why': 0.39; 'received:209': 0.40; 'to:addr:python.org': 0.40; '2011': 0.61; 'kind': 0.61; 'delaney': 0.84; 'idiom': 0.84; 'it"': 0.84; 'subject:Make': 0.96 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type:content-transfer-encoding; bh=LzfsszJQU+skBehmbQH5/A8X+RkC210q7j/SBa0OSQs=; b=j7TeQVN1ZBpYXxZRaGQhAABjoTUFp+VkyBhVLEsKtYcaC5k4sdyTW9CN3ThGW3/o1Q bEtdgmeWCx3+W7WCwkXueCtP+guhbE4OhzvdsuH5S/37tT2nCEZZLZsfiGrhbrJUgAiz RY6jH93GygAACh0GhfL5m+pxQuKfQTy6nUba0= MIME-Version: 1.0 In-Reply-To: References: <10d3d04f-1708-4942-8e69-92b715f01ff8@p20g2000vbm.googlegroups.com> <1324042394.23801.6.camel@tim-laptop> <4eebfbff$0$1679$742ec2ed@news.sonic.net> <17238811.518.1324198329306.JavaMail.geo-discussion-forums@yqio4> From: Ian Kelly Date: Sun, 18 Dec 2011 23:57:43 -0700 Subject: Re: Make a small function thread safe To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 58 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1324277895 news.xs4all.nl 6969 [2001:888:2000:d::a6]:36716 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:17501 On Sun, Dec 18, 2011 at 6:27 PM, Tim Delaney wrote: > On 18 December 2011 19:52, RangerElf wrote: >> >> Which is why the original .acquire() ... .release() idiom was wrong, thi= s >> would better express the intent: >> >> try: >> =A0lock.acquire() >> =A0shared_container.append(...) >> finally: >> =A0lock.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 rele= ase > a lock that you have not acquired. > > Which again is why using with is a much better option - you can't make th= is > 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 =3D 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 "", line 1, in File "/usr/lib64/python2.7/contextlib.py", line 17, in __enter__ return self.gen.next() File "", line 7, in bad_context_manager File "", 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.