Path: csiph.com!eternal-september.org!feeder.eternal-september.org!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!nzpost1.xs4all.net!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:skip:s 10': 0.05; 'difference,': 0.07; 'implements': 0.07; 'socket': 0.07; 'variant': 0.07; 'cc:addr:python-list': 0.09; '%r"': 0.09; 'guarded': 0.09; 'nameerror:': 0.09; 'port))': 0.09; 'thread': 0.10; 'python': 0.10; 'def': 0.13; 'ignore': 0.14; 'behaviour.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'ideally,': 0.16; 'interrupted.': 0.16; 'monitors': 0.16; 'polling.': 0.16; 's.close()': 0.16; 'stdin': 0.16; 'threading': 0.16; 'wake': 0.16; 'windows:': 0.16; 'wrote:': 0.16; "shouldn't": 0.18; 'try:': 0.18; 'input': 0.18; 'windows': 0.20; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'meant': 0.22; 'affects': 0.22; 'globally.': 0.22; 'sep': 0.22; 'pass': 0.22; 'code,': 0.23; 'code.': 0.23; 'second': 0.24; 'import': 0.24; 'patch': 0.24; 'unix': 0.24; 'header:In-Reply-To:1': 0.24; 'sort': 0.25; "doesn't": 0.26; 'chris': 0.26; 'skip:" 20': 0.26; 'checking': 0.27; 'fri,': 0.27; 'skip:t 40': 0.27; 'message- id:@mail.gmail.com': 0.27; 'blocking': 0.29; 'cpu': 0.29; 'measure': 0.29; 'sleep': 0.29; 'allows': 0.30; 'connection': 0.30; 'code': 0.30; "i'd": 0.31; 'though,': 0.32; 'exhibit': 0.33; 'except': 0.34; 'add': 0.34; 'received:google.com': 0.35; 'path': 0.35; 'pm,': 0.36; 'subject:: ': 0.37; 'difference': 0.38; 'itself': 0.38; "won't": 0.38; 'version': 0.38; 'skip:s 40': 0.38; 'whatever': 0.39; 'rather': 0.39; 'some': 0.40; 'future': 0.60; 'ten': 0.60; 'your': 0.60; 'back': 0.62; 'per': 0.62; 'times': 0.63; 'other.': 0.64; 'periodically': 0.66; 'money': 0.71; 'increase': 0.73; 'chrisa': 0.84; 'complexity': 0.84; 'effectively,': 0.84; 'waking': 0.84; 'to:none': 0.91; 'edwards': 0.91 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=QneqqmwU4RjIx/KUq/dkC8pZBlLoWv3Cg04GoQpHUoA=; b=Ru0N/dxxQ0jN9uznnr03QISJCycKG1m+ayMAJGS7zSbmZu6BgcBspR35m8RbRPl2/T 6Iy7OSilgHtpv5afJVu9GrOkV9C9J7Ufm8/KH0hO6i3dO993PLHGsAy17N5m30qRslZW VlJ5k5Ihe+kKmalMeJxTDqjBNsDe9HWT0JLQJzn/wNH3Wogh6JDx7ZeaiaFYLy0LBn24 peDRPQTOTp44TdB9EBqm9DuojscMe4FoOi+XQXhdV+Pj2feX4E8y+lKWqFOniWjDbiGu rRIeoysW2mZa/8eI+imXCM8VuH3H+5Xenn8pUVSuZyVxrURH0z/Vs0P/FunYSLtuyzmY wpkw== MIME-Version: 1.0 X-Received: by 10.107.132.144 with SMTP id o16mr4323889ioi.31.1441981663302; Fri, 11 Sep 2015 07:27:43 -0700 (PDT) In-Reply-To: References: Date: Sat, 12 Sep 2015 00:27:43 +1000 Subject: Re: Signal SIGINT ignored during socket.accept 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.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: 67 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1441981666 news.xs4all.nl 23782 [2001:888:2000:d::a6]:46865 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:96344 On Fri, Sep 11, 2015 at 11:50 PM, Grant Edwards wrote: > On 2015-09-11, Chris Angelico wrote: > >> This is what I meant when I said you would be polling. Effectively, >> you wake up your program every half-second, check if Ctrl-C has been >> pressed, and if it hasn't, you go back to sleep again. This is pretty >> inefficient. > > Though it offends one's engineering sensibilities[1], it's just not > that inefficient. I'd bet money you won't even be able to measure the > difference in CPU usage. Waking up twice per second and immediately > calling select() again on any hardware/OS built in the past 50 years > is going completely negligible (as long as you can ignore the smell). > > Even waking up ten times per second won't be noticeable. > True (although, as Marko says, it can add up). The other difference, though, is that I like to keep "cope-with-stupidity" code to itself - ideally, the clean path shouldn't be infected with it. Waking up periodically with select(), when you otherwise just want a blocking accept(), affects all your code. Spinning off a thread that monitors stdin and signals the main thread when it's time to shut down can be kept to a single block of code, guarded by some sort of platform check: import socket PORT = 8880 mainsock = socket.socket() mainsock.bind(("", PORT)) mainsock.listen(1) if Windows: # however you want to go about checking this import threading # Python 2/3 compat try: input = raw_input except NameError: pass def console(): """Constantly read from stdin and discard""" try: while "moar console": input() except (KeyboardInterrupt, EOFError): socket.socket().connect(("127.0.0.1",PORT)) threading.Thread(target=console).start() while "moar sockets": s = mainsock.accept() print("New connection: %r" % s) # Do whatever you want with the connection s.close() As well as keeping the Unix variant stupidity-free, this allows you to vary your platform check in the event that a future version of Windows allows blocking calls to be interrupted. Or if a future version of Python implements this kind of check globally. Or if a Python built with Cygwin doesn't exhibit this behaviour. Or if PyPy... you get the idea. The clean path is clearly delineated, and hopefully, the complexity of your code will increase linearly with the number of problems you're coping with, rather than having each patch affect each other. ChrisA