Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #17501
| 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 | <ian.g.kelly@gmail.com> |
| 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 | <CAN8CLgm6LjCn4JB7+CytrfRo9r3bZD1evYWw8QwFp45O+AYYCg@mail.gmail.com> |
| References | <10d3d04f-1708-4942-8e69-92b715f01ff8@p20g2000vbm.googlegroups.com> <1324042394.23801.6.camel@tim-laptop> <CAF-oDbFg_yuMB=vgz8ZhmWHxTqY7A7JOkXFWbERyioHvLa=h2Q@mail.gmail.com> <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 | 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 <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3816.1324277895.27778.python-list@python.org> (permalink) |
| 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 |
Show key headers only | 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 | Next — Previous in thread | Next in thread | Find similar | Unroll 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