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: 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: References: <53E4CFD9.4080209@stoneleaf.us> From: Chris Kaynor 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" 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 --047d7b86cf26faf5c2050022ebed Content-Type: text/plain; charset=UTF-8 On Fri, Aug 8, 2014 at 11:35 AM, Neil D. Cerutti wrote: > On 8/8/2014 12:16 PM, Chris Angelico wrote: > >> 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? >> > > 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 > > --047d7b86cf26faf5c2050022ebed Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
On F= ri, 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:
=C2=A0 =C2=A0 =C2=A0acquired =3D lock.acquire(blocking=3DFalse)
=C2=A0 =C2=A0 =C2=A0if acquired:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0stack.callback(lock.release)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0do_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=3DFalse suffer from the sam= e 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=3DTrue, timeout=3D-1):
=C2=A0 =C2=A0 acquired =3D lock.acquire(blocking, timeout)
=C2=A0 =C2=A0 if acquired:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 yield acquired
=C2=A0 =C2=A0 =C2=A0 =C2=A0 lock.release()
=C2=A0 =C2=A0 else:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 yield acquired

<= div>What I'd probably do is:
@contextlib.contextmanager
de= f release_if_acquired(lock, blocking=3DTrue, timeout=3D-1):
=C2=A0 =C2= =A0 acquired =3D lock.acquire(blocking, timeout)
=C2=A0 =C2=A0 try:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 yield acquired
=
=C2=A0 =C2=A0 finally:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 if acquir= ed:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 lock.release()=C2=A0

However, there is still the chance that a interrupt= signal (ctrl+c) could prevent the lock from being released, but I think th= e only 100% solution would be to write the code in C where it cannot be int= errupted within Python. The OS could still interrupt or kill the thread, bu= t in that case, I don't think there is anything you can do...


with release_if_acquired(lock, blocking=3DFalse) as acquired:
=C2=A0 =C2=A0 if acquired:

=C2=A0 =C2=A0 =C2=A0 =C2=A0 do_stuff

=
--047d7b86cf26faf5c2050022ebed--