Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #75904
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!newsreader4.netcologne.de!news.netcologne.de!xlned.com!feeder7.xlned.com!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <ckaynor@zindagigames.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.003 |
| X-Spam-Evidence | '*H*': 0.99; '*S*': 0.00; 'python.': 0.02; 'else:': 0.03; 'context': 0.07; 'finally:': 0.07; 'callback': 0.09; 'release,': 0.09; 'try:': 0.09; 'def': 0.12; '"release': 0.16; 'defer': 0.16; 'stack:': 0.16; 'subject: \n ': 0.16; 'subject:lock': 0.16; 'subject:when': 0.16; 'thread,': 0.16; 'timeout)': 0.16; 'two.': 0.16; 'unexpected': 0.16; '\xc2\xa0if': 0.16; 'exception': 0.16; 'prevent': 0.16; 'sat,': 0.16; 'wrote:': 0.18; 'acquired': 0.19; '>>>': 0.22; 'aug': 0.22; 'to:name:python- list@python.org': 0.22; 'header:In-Reply-To:1': 0.27; 'correct': 0.29; 'chris': 0.29; 'am,': 0.29; "doesn't": 0.30; 'message- id:@mail.gmail.com': 0.30; 'code': 0.31; 'probably': 0.32; 'another': 0.32; 'received:209.85.212': 0.32; 'fri,': 0.33; "i'd": 0.34; 'could': 0.34; 'received:209.85': 0.35; 'common': 0.35; 'case,': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'yield': 0.36; 'received:209': 0.37; 'being': 0.38; 'skip:& 10': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'anything': 0.39; 'to:addr:python.org': 0.39; 'signal': 0.60; 'happen': 0.63; 'chance': 0.65; 'within': 0.65; 'between': 0.67; 'natural': 0.68; '100%': 0.77; 'atomic': 0.84; 'interrupt': 0.84; 'suffer': 0.93; 'race': 0.95; 'interrupted': 0.96 |
| X-Google-DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:content-type; bh=BXt5ByVtqaoGmEdrPoW1bckuh66CZzn1hRJuljKhZEM=; b=DgTXBmDu5RN8mrwYzTZ7uJn6nJsZDQ39Sc0hel9tjgHekBy0EO0sLideWGVIpxCBDQ sbZymaVUN4/iRo4kTiebfYkLRPocs1c8QikVcquLkZN8TcUuzu2/kAJloXUonEII9cI1 5vshi+xnVHiS/T4VoF8DEO3n6MWTctVXiY+iawJN9NqdQ/OtkiCRf//RPGO28M+ONL2Y o1juzmc0ZgzMFeQ8n5YqPoe+xaZGMZ8ALsRmJwAd4TqKLCZo8FWJa1pU+ZA6w42SoKZD nJ1zKjfsiuecoKKoURfwxIXt5PWozWrJA8nvu14HghscavP3L9RnmYiWNoz+30VnRzxW 7OfA== |
| X-Gm-Message-State | ALoCoQlVR+z95BBH9yUfMeuOyZLEmF/HqBJksi1eJu10JxROxJ06Mq/Ss19e2lDmaJ9VPtC4yYCA |
| X-Received | by 10.194.58.244 with SMTP id u20mr34605920wjq.36.1407524871522; Fri, 08 Aug 2014 12:07:51 -0700 (PDT) |
| MIME-Version | 1.0 |
| In-Reply-To | <ls359h$tqv$1@ger.gmane.org> |
| References | <ff17939d-84cc-4f86-a5b9-9056d76dc9c0@googlegroups.com> <53E4CFD9.4080209@stoneleaf.us> <ls2sh7$eip$1@ger.gmane.org> <CAPTjJmpvfrEPc3tGVfhNS1W2bo9B4QR7AkK99yxMyOZND4woNQ@mail.gmail.com> <ls359h$tqv$1@ger.gmane.org> |
| From | Chris Kaynor <ckaynor@zindagigames.com> |
| Date | Fri, 8 Aug 2014 12:07:30 -0700 |
| Subject | Re: Specifying `blocking` and `timeout` when acquiring lock as a context manager |
| To | "python-list@python.org" <python-list@python.org> |
| Content-Type | multipart/alternative; boundary=047d7b86cf26faf5c2050022ebed |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://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 | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.12760.1407525364.18130.python-list@python.org> (permalink) |
| Lines | 136 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1407525364 news.xs4all.nl 2857 [2001:888:2000:d::a6]:59346 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:75904 |
Show key headers only | View raw
[Multipart message — attachments visible in raw view] - view raw
On Fri, Aug 8, 2014 at 11:35 AM, Neil D. Cerutti <neilc@norwich.edu> wrote:
> On 8/8/2014 12:16 PM, Chris Angelico wrote:
>
>> On Sat, Aug 9, 2014 at 2:05 AM, Neil D. Cerutti <neilc@norwich.edu>
>> wrote:
>>
>>> Perhaps defer release, a la a common Go pattern:
>>>
>>> with contextlib.ExitStack() as stack:
>>> acquired = lock.acquire(blocking=False)
>>> if acquired:
>>> stack.callback(lock.release)
>>> do_stuff
>>>
>>
>> There's a race condition in that - an unexpected exception could
>> happen between those two. Are you able to set the callback to be a
>> "release if acquired" atomic operation?
>>
>
> Doesn't any natural looking use of blocking=False suffer from the same
> race condition? What's the correct way to use it?
>
> Here's another attempt at context managing:
>
> @contextlib.contextmanager
> def release_if_acquired(lock, blocking=True, timeout=-1):
> acquired = lock.acquire(blocking, timeout)
> if acquired:
> yield acquired
> lock.release()
> else:
> yield acquired
>
What I'd probably do is:
@contextlib.contextmanager
def release_if_acquired(lock, blocking=True, timeout=-1):
acquired = lock.acquire(blocking, timeout)
try:
yield acquired
finally:
if acquired:
lock.release()
However, there is still the chance that a interrupt signal (ctrl+c) could
prevent the lock from being released, but I think the only 100% solution
would be to write the code in C where it cannot be interrupted within
Python. The OS could still interrupt or kill the thread, but in that case,
I don't think there is anything you can do...
> with release_if_acquired(lock, blocking=False) as acquired:
> if acquired:
>
> do_stuff
>
>
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Specifying `blocking` and `timeout` when acquiring lock as a context manager cool-RR <ram.rachum@gmail.com> - 2014-08-08 04:51 -0700 Re: Specifying `blocking` and `timeout` when acquiring lock as a context manager Ethan Furman <ethan@stoneleaf.us> - 2014-08-08 06:25 -0700 Re: Specifying `blocking` and `timeout` when acquiring lock as a context manager "Neil D. Cerutti" <neilc@norwich.edu> - 2014-08-08 12:05 -0400 Re: Specifying `blocking` and `timeout` when acquiring lock as a context manager Chris Angelico <rosuav@gmail.com> - 2014-08-09 02:16 +1000 Re: Specifying `blocking` and `timeout` when acquiring lock as a context manager "Neil D. Cerutti" <neilc@norwich.edu> - 2014-08-08 14:35 -0400 Re: Specifying `blocking` and `timeout` when acquiring lock as a context manager "Neil D. Cerutti" <neilc@norwich.edu> - 2014-08-08 14:57 -0400 Re: Specifying `blocking` and `timeout` when acquiring lock as a context manager Chris Kaynor <ckaynor@zindagigames.com> - 2014-08-08 12:07 -0700 Re: Specifying `blocking` and `timeout` when acquiring lock as a context manager Ian Kelly <ian.g.kelly@gmail.com> - 2014-08-08 13:21 -0600 Re: Specifying `blocking` and `timeout` when acquiring lock as a context manager Chris Angelico <rosuav@gmail.com> - 2014-08-09 08:57 +1000
csiph-web