Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #102707
| Path | csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | "Frank Millman" <frank@chagford.com> |
| Newsgroups | comp.lang.python |
| Subject | asyncio and blocking - an update |
| Date | Tue, 9 Feb 2016 09:33:49 +0200 |
| Lines | 40 |
| Message-ID | <mailman.0.1455003248.29838.python-list@python.org> (permalink) |
| Mime-Version | 1.0 |
| Content-Type | text/plain; format=flowed; charset="iso-8859-1"; reply-type=original |
| Content-Transfer-Encoding | 7bit |
| X-Trace | news.uni-berlin.de GcQbT2EIlEwzokJHyZBghwwqh8FhYLlthALdJzhei+UQ== |
| Return-Path | <python-python-list@m.gmane.org> |
| 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; 'yet.': 0.03; 'handler': 0.04; 'modify': 0.04; 'test,': 0.05; 'tries': 0.05; 'method,': 0.07; 'reason,': 0.07; 'block.': 0.09; 'cursor': 0.09; 'iterate': 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; 'iterator': 0.16; '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; 'stick': 0.18; 'tests': 0.18; 'pass': 0.22; 'bit': 0.23; 'seems': 0.23; 'tried': 0.24; 'written': 0.24; 'requests': 0.25; 'header:X-Complaints-To:1': 0.26; 'sense': 0.26; 'separate': 0.27; 'switch': 0.27; 'function': 0.28; 'idea': 0.28; "skip:' 10": 0.28; 'queue': 0.29; 'subject:update': 0.29; 'table,': 0.29; 'thread,': 0.29; 'run': 0.33; 'problem': 0.33; 'recommended': 0.34; 'that,': 0.34; 'list': 0.34; 'could': 0.35; 'but': 0.36; 'list,': 0.36; 'there': 0.36; 'possible': 0.36; 'to:addr:python-list': 0.36; 'really': 0.37; 'received:org': 0.37; 'wanted': 0.37; 'end': 0.39; 'sure': 0.39; 'does': 0.39; 'to:addr:python.org': 0.40; 'some': 0.40; 'back': 0.62; 'skip:n 10': 0.62; 'matter': 0.63; 'benefit': 0.66; 'effective.': 0.66; 'here': 0.66; 'results': 0.66; 'frank': 0.72; '100%': 0.72; 'heavy': 0.81; 'abandon': 0.84; 'faster.': 0.84; 'approach.': 0.91 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| X-Gmane-NNTP-Posting-Host | 197.89.154.62 |
| 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 <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Xref | csiph.com comp.lang.python:102707 |
Show key headers only | View raw
Hi all 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. 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. It would not matter so much if I used cur.fetchall(), and sent all the rows back in one 'put', but I really wanted to iterate over the cursor and 'put' a row at a time. The idea was that it would be truly asynchronous from end to end - the database handler would retrieve rows one at a time, and via the queue my app would process them one at a time, all without blocking. I can switch to fetchall(), but then there is no benefit over the recommended approach. Although fetchall() is non-blocking, once the rows are received as a list the function that processes them effectively does block. If that became a problem one could use an Asynchronous Iterator to process the list, but I have not had that need yet. Writing timing tests is tricky. It is possible that under some circumstances, with a heavy load, a large table, and a time-consuming function to process each row, the overhead of call_soon_threadsafe() would be minimised and my approach might be effective. For now, however, I will regretfully abandon my approach and stick with run_in_executor(). Frank Millman
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
asyncio and blocking - an update "Frank Millman" <frank@chagford.com> - 2016-02-09 09:33 +0200
csiph-web