Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: "Frank Millman" Newsgroups: comp.lang.python Subject: Re: asyncio and blocking - an update Date: Thu, 11 Feb 2016 07:45:16 +0200 Lines: 36 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; format=flowed; charset="iso-8859-1"; reply-type=response Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de 618mC9SRqTK37S1ErLiZuA7s+mreaQReRK79PSekAMpw== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'handler': 0.04; 'modify': 0.04; 'test,': 0.05; 'tries': 0.05; 'method,': 0.07; 'reason,': 0.07; 'block.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'rows,': 0.09; "skip:' 30": 0.15; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'row': 0.16; 'thread.': 0.16; 'app': 0.16; 'solution.': 0.18; 'pass': 0.22; 'bit': 0.23; 'seems': 0.23; 'wrote': 0.23; 'slightly': 0.23; 'tried': 0.24; 'written': 0.24; 'header:In-Reply-To:1': 0.24; 'requests': 0.25; 'header:X-Complaints-To:1': 0.26; 'sense': 0.26; 'separate': 0.27; "skip:' 10": 0.28; '50,': 0.29; 'subject:update': 0.29; 'thread,': 0.29; 'run': 0.33; 'problem': 0.33; 'recommended': 0.34; 'that,': 0.34; 'but': 0.36; 'instead': 0.36; 'there': 0.36; 'faster': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'sure': 0.39; 'does': 0.39; 'build': 0.40; 'to:addr:python.org': 0.40; 'some': 0.40; 'increased': 0.61; 'provide': 0.61; 'back': 0.62; 'more': 0.63; 'here': 0.66; 'results': 0.66; 'frank': 0.72; '100%': 0.72; '100': 0.79; 'faster.': 0.84; 'optimum': 0.93 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: 197.89.154.180 In-Reply-To: X-MSMail-Priority: Normal Importance: Normal X-Newsreader: Microsoft Windows Live Mail 15.4.3502.922 X-MimeOLE: Produced By Microsoft MimeOLE V15.4.3502.922 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21rc2 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:102786 "Frank Millman" wrote in message news:n9c4p3$gmp$1@ger.gmane.org... > > Some of you may have been following my attempts to modify my asyncio app > so that it does not block when accessing the database. Here is an update. > Here is an update to my update ... > I came up with what felt like a good idea. Run the database handler in a > separate thread, pass requests to it using a queue.Queue, and get it to > pass results back using an asyncio.Queue. > > It works, but I had a vague sense that performance was a bit sluggish, so > I tried the 'recommended' approach of using asyncio.run_in_executor() to > execute database calls in a separate thread. It felt a bit faster. > > Now I have written a proper timing test, and the recommended approach is > much faster. I am not 100% sure of the reason, but I think the problem is > that, with my method, when the database tries to 'put' a row on the return > queue, it has to use 'loop.call_soon_threadsafe()', and this seems to > create a bottleneck. > I have come up with a plan that seems to provide a solution. Instead of 'putting' one row at a time, let the database handler build up a block of rows, and then 'put' the block. I tried a block of 10, and it ran a lot faster. I increased it to 50, and it ran faster again. I tried 100 and there was not much improvement, so 50 seems like an optimum number. The speed is now only slightly slower than run_in_executor(), and it is more truly asynchronous. Frank