Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed2.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.087 X-Spam-Evidence: '*H*': 0.83; '*S*': 0.00; 'socket': 0.07; 'url:github': 0.09; 'subject:How': 0.10; 'cc:addr:python-list': 0.11; 'python': 0.11; "wouldn't": 0.14; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'non-blocking': 0.16; 'sockets': 0.16; 'subject:non': 0.16; 'timeout': 0.16; 'url:linux': 0.16; 'subject: ?': 0.16; 'wrote:': 0.18; 'written': 0.21; 'feb': 0.22; 'handles': 0.22; 'cc:addr:python.org': 0.22; '2.x': 0.24; 'connected': 0.24; 'fairly': 0.24; 'cc:2**0': 0.24; "i've": 0.25; 'header:In-Reply-To:1': 0.27; 'room': 0.29; "doesn't": 0.30; 'message-id:@mail.gmail.com': 0.30; 'along': 0.30; 'lines': 0.31; 'that.': 0.31; 'exceptions': 0.31; 'languages': 0.32; 'stuff': 0.32; 'comment': 0.34; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'data,': 0.36; 'possible': 0.36; 'similar': 0.36; 'being': 0.38; 'server': 0.38; 'generic': 0.38; 'handle': 0.38; 'pm,': 0.38; 'anything': 0.39; 'catch': 0.60; 'new': 0.61; 'matter': 0.61; 'simply': 0.61; 'networking': 0.64; 'more': 0.64; 'here': 0.66; 'listening': 0.74; 'low': 0.83; '"hey,': 0.84; '2015': 0.84; 'asynchronous': 0.84; 'everything,': 0.84; 'pike': 0.84; 'facilities.': 0.91; 'subject:skip:S 20': 0.91; 'system:': 0.91; 'to:none': 0.92; 'clients,': 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=qoxle3itwCuZHaez7dGOw6QNGwQMFv1UnVmtSn1ZxC0=; b=KiQ65iLAIMOhGanTwstEm+Jnoh35Dw+O1cvSwPpkR39KYx1o3RI50xkriOLHPZivZI OUqRo0MACBzia/1yey4RE6CNTmYI9Z1T9DLYM6t3TX6KghSRHrHW5m8YsO37OCmsaCTZ FxHvFsCPGvxKbyo+xKozcKvxt8zIVR+RJZWWT7yq2VGAkndjQO6pXHKML7v9dsvL643D L6szWt+bfzkEy2TOLvYtKCSsMjpuznXMyUYoHy1Go7ci79+J40mn700wM6CNCHZRE5dP Ik/Jn+DTAnK3bl7xOicidVmSEADl0awGuDvpytGr0lMRlbPOAuJJKhbowRW41+YthbnJ nfiA== MIME-Version: 1.0 X-Received: by 10.50.18.108 with SMTP id v12mr17678489igd.34.1422969691784; Tue, 03 Feb 2015 05:21:31 -0800 (PST) In-Reply-To: References: <47031e69-e94d-4257-8c7d-e7c00a543634@googlegroups.com> <874mr3ibve.fsf@elektro.pacujo.net> <87r3u7grd3.fsf@elektro.pacujo.net> Date: Wed, 4 Feb 2015 00:21:31 +1100 Subject: Re: How to write a non blocking SimpleHTTPRequestHandler ? 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: 33 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1422971344 news.xs4all.nl 2918 [2001:888:2000:d::a6]:57644 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:85146 On Tue, Feb 3, 2015 at 11:56 PM, Yassine Chaouche wrote: > But your comment is interesting because, as I understand it, a non-blocking web server is simply a matter of setting timeouts on sockets, catch the exceptions and move on. I don't know why wouldn't that be possible with python stdlib ? > Not really. You could, in theory, set very low timeouts and then poll everything, but it's not efficient. What you want to do is say to the system "Hey, see all these sockets? Let me know when *any one of them* has stuff for me", where "stuff" would be a new connected client if it's a listening socket, or some data written if it's a connected socket; and you might need to check if there's room to write more data, too, which you can do with the same syscall. The key here is that you have a long timeout on the meta-event "any one of these being ready". That's not simply a matter of setting socket timeouts; you need a way to handle the meta-event, and that's something along the lines of select(): http://linux.die.net/man/2/select Other languages have inbuilt asynchronous I/O handlers; eg Pike handles this fairly well, and I've made some use of it with a generic networking system: https://github.com/Rosuav/Hogan Basically, you spin up a server with any number of listening sockets, each of which can talk to any number of connected clients, and all of those sockets get smoothly multiplexed on a single thread. Lots of other languages have similar facilities. Python 2.x doesn't have anything of that nature; Python 3's asyncio is exactly that. ChrisA