Path: csiph.com!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed8.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; '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 Cc: "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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 On Fri, Aug 7, 2015 at 10:34 AM, wrote: > Despite my "except KeyboardInterrupt", the KeyboardInterrupt forced by th= e thread.interrupt_main() in the worker thread isn't being caught. > > Other things worth noting is that the exception takes about 3 seconds aft= er 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 re= moved, 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 h= ave 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 =3D False while not interrupt: try: connection, _ =3D 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 separate= ly. 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