Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!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.023 X-Spam-Evidence: '*H*': 0.95; '*S*': 0.00; 'syntax': 0.04; 'anyway.': 0.05; '101': 0.09; 'apis': 0.09; 'rewrite': 0.09; 'cc:addr:python- list': 0.11; 'def': 0.12; 'thread': 0.14; '*must*': 0.16; 'above?': 0.16; 'apis.': 0.16; 'blocking': 0.16; 'concurrent': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'internally': 0.16; 'limit,': 0.16; 'reasonably': 0.16; 'threads.': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'commit': 0.19; 'memory': 0.22; 'otherwise,': 0.22; 'separate': 0.22; 'cc:addr:python.org': 0.22; 'logical': 0.24; "shouldn't": 0.24; 'fairly': 0.24; '(or': 0.24; 'cc:2**0': 0.24; 'query': 0.26; 'header:In-Reply-To:1': 0.27; 'idea': 0.28; 'chris': 0.29; 'possibility': 0.29; 'scale': 0.29; 'database,': 0.30; 'matching': 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'work.': 0.31; 'getting': 0.31; 'requests': 0.31; 'purely': 0.31; 'probably': 0.32; 'implemented': 0.33; 'connection': 0.35; 'transaction': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'really': 0.36; 'should': 0.36; 'half': 0.37; 'unit': 0.37; 'turn': 0.37; 'two': 0.37; 'easily': 0.37; 'handle': 0.38; 'pm,': 0.38; 'ability': 0.39; 'realize': 0.39; 'major': 0.40; 'how': 0.40; 'skip:u 10': 0.60; 'around.': 0.60; 'dangerous': 0.60; 'most': 0.60; 'numbers': 0.61; "you'll": 0.62; 'happen': 0.63; 'close': 0.67; 'advantages': 0.68; 'delegate': 0.68; 'request.': 0.70; 'million': 0.74; 'asynchronous': 0.84; 'desperately': 0.84; 'forced': 0.84; 'issues;': 0.84; 'inefficient': 0.91; 'transactions': 0.91; 'to:none': 0.92; 'connection,': 0.95; 'hundred': 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=k3AZyWQP7iBXx1JGla4mSH7c+ZulPEOpfb0cmsIBbSQ=; b=ClpU52TgEElU0o9D9/s6+6594GajrrBncp+QTpSlRD5oVvdBSskxS0ylXs9CrmVEMB YYTpIBZiPq22dudVi/upA6brRccHBVNXh1nrLyQY1pH73dqXDQbaa9KWRflXvEnedpCr qx9EYlhdT5aiczDQaPz1K1anojN2gXXQlLayfXxNmw8IxDhj5+xR2eVBdPSLpPYc0j2x 40uT3V5InvIX9iSk+vkPe1y1w6P9gZmgCyuJCkwSZaXdUAMEHUaBC1L0C/jsfPIgJ48X ZnJ2EeHWxnIuobkafDvMPtyrm4xerKwNN0ay4JGPOxTVLhzUCfrsLc/IsMLzDLsg3/0k Uhsg== MIME-Version: 1.0 X-Received: by 10.52.167.132 with SMTP id zo4mr20164vdb.97.1401787814495; Tue, 03 Jun 2014 02:30:14 -0700 (PDT) In-Reply-To: <87wqcyuznv.fsf@elektro.pacujo.net> References: <874n03t5t9.fsf@elektro.pacujo.net> <7x4n03dor0.fsf@ruckus.brouhaha.com> <878upe8poc.fsf@elektro.pacujo.net> <87wqcyuznv.fsf@elektro.pacujo.net> Date: Tue, 3 Jun 2014 19:30:14 +1000 Subject: Re: Benefits of asyncio 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: 55 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1401787816 news.xs4all.nl 2887 [2001:888:2000:d::a6]:60023 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:72496 On Tue, Jun 3, 2014 at 7:10 PM, Marko Rauhamaa wrote: > Chris Angelico : > >> def request.process(self): # I know this isn't valid syntax >> db.act(whatever) # may block but shouldn't for long >> db.commit() # ditto >> write(self, response) # won't block >> >> This works as long as your database is reasonably fast and close > > I find that assumption unacceptable. It is a dangerous assumption. > The DB APIs desperately need asynchronous variants. As it stands, you > are forced to delegate your DB access to threads/processes. > >> So how do you deal with the possibility that the database will block? > > You separate the request and response parts of the DB methods. That's > how it is implemented internally anyway. > > Say no to blocking APIs. Okay, but how do you handle two simultaneous requests going through the processing that you see above? You *MUST* separate them onto two transactions, otherwise one will commit half of the other's work. (Or are you forgetting Databasing 101 - a transaction should be a logical unit of work?) And since you can't, with most databases, have two transactions on one connection, that means you need a separate connection for each request. Given that the advantages of asyncio include the ability to scale to arbitrary numbers of connections, it's not really a good idea to then say "oh but you need that many concurrent database connections". Most systems can probably handle a few thousand threads without a problem, but a few million is going to cause major issues; but most databases start getting inefficient at a few thousand concurrent sessions. >> but otherwise, you would need to completely rewrite the main code. > > That's a good reason to avoid threads. Once you realize you would have > been better off with an async approach, you'll have to start over. You > can easily turn a nonblocking solution into a blocking one but not the > other way around. Alright. I'm throwing down the gauntlet. Write me a purely nonblocking web site concept that can handle a million concurrent connections, where each one requires one query against the database, and one in a hundred of them require five queries which happen atomically. I can do it with a thread pool and blocking database queries, and by matching the thread pool size and the database concurrent connection limit, I can manage memory usage fairly easily; how do you do it efficiently with pure async I/O? ChrisA