Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #95097

Re: except block isn't catching exception

Path csiph.com!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
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; 'cc:addr:python-list': 0.09; 'back-end': 0.09; 'blocked': 0.09; 'loop.': 0.09; 'threads,': 0.09; 'timeout': 0.09; 'underlying': 0.09; 'thread': 0.10; 'python': 0.10; 'exception': 0.13; '"hey': 0.16; 'delay,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'removed,': 0.16; 'shorten': 0.16; 'subject:exception': 0.16; 'thread.': 0.16; 'timeout,': 0.16; 'wake': 0.16; 'wrote:': 0.16; 'merge': 0.18; 'try:': 0.18; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'aug': 0.20; 'handles': 0.20; 'saying': 0.22; '(on': 0.22; 'pass': 0.22; 'am,': 0.23; 'appears': 0.23; 'this:': 0.23; 'unix': 0.24; 'header:In-Reply-To:1': 0.24; 'all.': 0.24; "doesn't": 0.26; 'checking': 0.27; 'equivalent': 0.27; 'fri,': 0.27; 'separate': 0.27; 'message- id:@mail.gmail.com': 0.27; 'this.': 0.28; 'actual': 0.28; 'blocking': 0.29; 'directly,': 0.29; 'periodic': 0.29; 'thread,': 0.29; 'windows,': 0.29; 'handled': 0.29; 'call.': 0.30; 'seconds': 0.31; 'another': 0.32; 'run': 0.33; 'similar': 0.33; 'except': 0.34; 'received:google.com': 0.35; 'behind': 0.35; 'next': 0.35; 'could': 0.35; 'acceptable': 0.35; 'false': 0.35; 'something': 0.35; "isn't": 0.35; 'but': 0.36; 'instead': 0.36; 'flow': 0.36; 'framework': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'two': 0.37; 'being': 0.37; 'setting': 0.37; 'things': 0.38; 'monitor': 0.38; 'version': 0.38; 'means': 0.39; 'system.': 0.39; 'takes': 0.39; 'still': 0.40; 'called': 0.40; 'waiting': 0.60; 'your': 0.60; "you'll": 0.61; 'details': 0.62; 'times': 0.63; 'date,': 0.66; 'periodically': 0.66; 'worth': 0.67; 'wish': 0.71; 'connection,': 0.72; 'choices:': 0.84; 'chrisa': 0.84; 'flag.': 0.84; 'forced': 0.84; 'to:none': 0.91; 'interrupt': 0.91; 'choice.': 0.93; 'processes,': 0.93
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:content-transfer-encoding; bh=aTgkDhbQ52ADWREtH6BHj7p34n5hIWeqD/Z6Zlr6uKU=; b=lUBob1iG6BfBjipC1FzX4Xb/dg8tgwNVp0GXrIaWwRx3xUevX3FQ16kG8MVJnoYij4 GFLeWohigY3sgjTV8Epu9UGySB/Rp3zRbSdFl4mflQWiXlFVWN7SHH+xdSJxrE9g/DJo r7mBEve9r7brbERjbwZ3M9ZINKFLmXsi9cW/FhdIrAhvu3pHKf+XInHJVHF7wcpiyYpC MiKdX2DE+MscmHpsM7B0hq/Ex/69MvbUqZPM006Jw2iTp1GblIzsVc8t1mkdTrZCQaOh 4F67/m7QLQ5EVhS50uPklafCDxkeOfSmy96D2Tu7YTC4eEt0Hz3FxnBiQahT3B+WcnFx 5dNg==
MIME-Version 1.0
X-Received by 10.107.31.134 with SMTP id f128mr5075432iof.19.1438908363653; Thu, 06 Aug 2015 17:46:03 -0700 (PDT)
In-Reply-To <48587161-1bd6-47e4-b2c9-7a37c60a59e5@googlegroups.com>
References <48587161-1bd6-47e4-b2c9-7a37c60a59e5@googlegroups.com>
Date Fri, 7 Aug 2015 10:46:03 +1000
Subject Re: except block isn't catching exception
From Chris Angelico <rosuav@gmail.com>
Cc "python-list@python.org" <python-list@python.org>
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding quoted-printable
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
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.1286.1438908366.3674.python-list@python.org> (permalink)
Lines 52
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1438908366 news.xs4all.nl 2847 [2001:888:2000:d::a6]:59736
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:95097

Show key headers only | View raw


On Fri, Aug 7, 2015 at 10:34 AM,  <sohcahtoa82@gmail.com> wrote:
> Despite my "except KeyboardInterrupt", the KeyboardInterrupt forced by the thread.interrupt_main() in the worker thread isn't being caught.
>
> Other things worth noting is that the exception takes about 3 seconds after the call to thread.interrupt_main().  It appears to not actually happen until the sock.accept() times out.  If the call to sock.settimeout(5) is removed, the KeyboardInterrupt never appears and flow is still blocked on the sock.accept().
>
> My Python version is 2.7.3.  I know its out of date, but I don't really have a choice.

As far as I know, the only effect of thread.interrupt_main() is to set
a flag saying "Hey main thread, next time you go checking for these
things, there's a KeyboardInterrupt waiting for you". It doesn't
actually send an OS-level signal, which means it cannot interrupt a
blocking call. So you have a few choices:

1) Shorten the timeout on sock.accept() to an (if you'll excuse the
pun) acceptable delay, and then simply check a flag. Something like
this:

interrupt = False
while not interrupt:
    try:
        connection, _ = sock.accept()
    except socket.timeout:
        # On timeout, check flag and keep sleeping
        pass

If you wish to also have an actual timeout, you could monitor that separately.

2) Instead of blocking on sock.accept directly, have another event
which the other thread can raise, which will wake the main thread. You
could use select.select() for this.

3) Bury the details of select.select() away behind a nice framework
like asyncio, merge your two threads, and run everything through a
back-end event loop.

4) Use separate processes, and signal the interrupt using an OS-level
notification (on Unix systems, SIGINT; on Windows, there's an
equivalent called BreakSignal or something). This will break out of
the underlying system call that handles accept().

thread.interrupt_main() is similar to just setting a global that you
periodically check, except that the periodic check is handled for you
by the system. That's really all.

ChrisA

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

except block isn't catching exception sohcahtoa82@gmail.com - 2015-08-06 17:34 -0700
  Re: except block isn't catching exception Chris Angelico <rosuav@gmail.com> - 2015-08-07 10:46 +1000
    Re: except block isn't catching exception sohcahtoa82@gmail.com - 2015-08-07 10:16 -0700
      Re: except block isn't catching exception Chris Angelico <rosuav@gmail.com> - 2015-08-08 12:44 +1000
      Re: except block isn't catching exception Ian Kelly <ian.g.kelly@gmail.com> - 2015-08-08 00:56 -0600
      Re: except block isn't catching exception Chris Angelico <rosuav@gmail.com> - 2015-08-08 17:08 +1000

csiph-web