Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder7.xlned.com!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.006 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'callback': 0.09; 'release,': 0.09; 'cc:addr:python-list': 0.11; '"release': 0.16; 'defer': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'stack:': 0.16; 'subject: \n ': 0.16; 'subject:lock': 0.16; 'subject:when': 0.16; 'two.': 0.16; 'unexpected': 0.16; 'exception': 0.16; 'sat,': 0.16; 'wrote:': 0.18; 'acquired': 0.19; 'aug': 0.22; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; 'could': 0.34; 'common': 0.35; 'received:google.com': 0.35; 'happen': 0.63; 'between': 0.67; 'atomic': 0.84; 'to:none': 0.92; 'race': 0.95 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=Fanm/bojI6yil2W/8cud4cOHJqWRp3u7dqoEjU1y9mQ=; b=R72EuUe4Zv55oblOmqSKpCrIpkQbhmSRH1EURDo7hhYxlV4npfhbxqd7DHHOm1Gv5E zEbIKTMvklpb+XKio/3n0YFk1PmlvexlzU0+F0eC4rdP6TE6ki0ie7Mhbnd6DnyBDsCS MAwf+CFd2oYi8+OVaKdIX7ltSnL/2Fi/Nl07483afcL4xohWiooDlrDVCwNy513Jp2I2 vLNu/W3lv9gvuBUeM+mVXxInxAEo7ARCtOW6ZM4rVJNBhFQwZkeZgQdkaokj1sAVoq80 w5MIS4qnsg0kcjUPO2UPT266xXltdd4ZSQ9Eih22G3W4234cOejVT+zR4VAnAL3Tp55A FS0w== MIME-Version: 1.0 X-Received: by 10.42.216.148 with SMTP id hi20mr13075134icb.12.1407514614197; Fri, 08 Aug 2014 09:16:54 -0700 (PDT) In-Reply-To: References: <53E4CFD9.4080209@stoneleaf.us> Date: Sat, 9 Aug 2014 02:16:54 +1000 Subject: Re: Specifying `blocking` and `timeout` when acquiring lock as a context manager From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 14 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1407514617 news.xs4all.nl 2898 [2001:888:2000:d::a6]:46068 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:75898 On Sat, Aug 9, 2014 at 2:05 AM, Neil D. Cerutti 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? ChrisA