Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.018 X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; 'callback': 0.09; 'high- level': 0.09; 'sure,': 0.09; 'used.': 0.09; 'subject:How': 0.10; 'cc:addr:python-list': 0.11; 'python': 0.11; 'mostly': 0.14; 'times,': 0.14; "wouldn't": 0.14; 'windows': 0.15; 'blocking': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'handlers.': 0.16; 'intrinsic': 0.16; 'models,': 0.16; 'non- blocking': 0.16; 'sockets': 0.16; 'subject:non': 0.16; 'think.': 0.16; 'subject: ?': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'wed,': 0.18; "python's": 0.19; 'things.': 0.19; 'feb': 0.22; 'code,': 0.22; 'platforms': 0.22; 'programming': 0.22; 'cc:addr:python.org': 0.22; "aren't": 0.24; 'certainly': 0.24; 'convenient': 0.24; 'lets': 0.24; '(or': 0.24; 'cc:2**0': 0.24; "i've": 0.25; 'defined': 0.27; 'header:In-Reply-To:1': 0.27; 'external': 0.29; 'am,': 0.29; 'mix': 0.30; 'programming.': 0.30; 'said,': 0.30; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; 'bad.': 0.31; 'bunch': 0.31; 'commonly': 0.31; 'exceptions': 0.31; 'another': 0.32; 'older': 0.33; 'used,': 0.33; 'comment': 0.34; 'problem': 0.35; 'common': 0.35; 'tool': 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'done': 0.36; 'doing': 0.36; 'useful': 0.36; 'possible': 0.36; 'sometimes': 0.38; 'server': 0.38; 'structure': 0.39; 'though,': 0.39; 'catch': 0.60; 'most': 0.60; 'effective': 0.61; 'matter': 0.61; 'strictly': 0.61; 'simply': 0.61; 'such': 0.63; 'networking': 0.64; 'more': 0.64; 'different': 0.65; 'facilities': 0.69; 'wish': 0.70; 'therefore': 0.72; '2015': 0.84; 'coexist': 0.84; 'concept.': 0.84; 'cpu,': 0.84; 'pike': 0.84; 'aka': 0.91; 'care,': 0.91; 'facilities.': 0.91; 'subject:skip:S 20': 0.91; 'to:none': 0.92; 'wanting': 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; bh=eOf61jH64KyosPel8ZeeuYj4NxVOB3hbEXJBSpvYlfQ=; b=l1Pi3Q/tl5YErLXy6MwuxSSoybWMBpTRnQwbRcQErkfd5cA3u9PZ8sVyAGqD0LIIfh 5jn7lGd8z0ydWQyhESq+qITjqwkMbef0cuWieVKXLtrIZSqVY9GD6WuNFPCb4GnhqIDq mXWYqsoBHKuqD/+NAKFLQMHn6fZEBn9ZnQnlU/5qtX3ziG45UmIxVlJiqkUhMa6RB38m SijZk5BWzIuAHBh24GwY7/1OKqxAUg1jTMXam4Rp8X5bNHyHNT0eEzCvUO19F5E/Fiqa kVgXt84IWULO37JLKzln73t37Bh3WP1m8/XrwOEJoKLiWTIVrRtx+twu9FtmDrxwymqj ogwg== MIME-Version: 1.0 X-Received: by 10.50.171.201 with SMTP id aw9mr18000416igc.2.1422972217327; Tue, 03 Feb 2015 06:03:37 -0800 (PST) In-Reply-To: <87iofjglbg.fsf@elektro.pacujo.net> References: <47031e69-e94d-4257-8c7d-e7c00a543634@googlegroups.com> <874mr3ibve.fsf@elektro.pacujo.net> <87r3u7grd3.fsf@elektro.pacujo.net> <87iofjglbg.fsf@elektro.pacujo.net> Date: Wed, 4 Feb 2015 01:03:37 +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: 45 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1422972220 news.xs4all.nl 2864 [2001:888:2000:d::a6]:35101 X-Complaints-To: abuse@xs4all.nl Path: csiph.com!usenet.pasdenom.info!bete-des-vosges.org!feed.ac-versailles.fr!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Xref: csiph.com comp.lang.python:85147 On Wed, Feb 4, 2015 at 12:45 AM, Marko Rauhamaa 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. > > Now I think you might have some misconceptions about nonblocking > networking I/O. Nonblocking I/O is done using asynchronous, or > event-driven, programming. Your code reacts to external stimuli, never > blocking, mostly just sleeping. The reactions are defined in callback > routings, aka listeners, aka event handlers. Not strictly true - that's just one convenient way of doing things. A callback/event-handler structure lets you write a bunch of listeners that coexist effortlessly, but it's not the only way to do non-blocking I/O, and it's certainly not an intrinsic part of the concept. That said, though, it is a VERY convenient way to lay things out in the code. The Pike system I offered, and most of the older multiplexed-I/O systems I've used, did work that way. It just isn't something that non-blocking necessarily implies. >> I don't know why wouldn't that be possible with python stdlib ? > > It is possible using the low-level facilities. However, the traditional > high-level facilities are built on multithreading, which (as a rule) is > based on blocking I/O. Multithreading is another way to cope with the same problem of wanting to deal with different sockets on a single CPU, but I don't think it's inherently a part of any of Python's own high-level facilities - not that I can think of, at least? However, in terms of common programming models, yes, multithreading+blocking I/O is an effective way to write code, and will therefore be commonly used. I wish more people had grown up on OS/2 instead of (or as well as) Windows or Unix. Threading is not such a bugbear as a lot of people seem to think. Yes, some platforms have traditionally had poor implementations, and to be sure, you don't want to mix threading and forking without a *lot* of care, but threads aren't inherently bad. They're a useful tool in the toolbox. Sometimes non-blocking I/O is the right thing to do; sometimes threads suit the problem better; other times, something else again. ChrisA