Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: "Frank Millman" Newsgroups: comp.lang.python Subject: Re: Question about asyncio and blocking operations Date: Wed, 27 Jan 2016 16:40:29 +0200 Lines: 86 Message-ID: References: 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 JIdi11u2CT/Y9M6IrOhVTApLgDNxY5M08m5hEsOljlMA== Return-Path: 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; 'subject:Question': 0.05; 'attributes': 0.07; 'practice,': 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; 'requested.': 0.09; 'rows': 0.09; 'vast': 0.09; 'assume': 0.11; 'jan': 0.11; 'exception': 0.13; 'def': 0.13; 'feedback.': 0.15; '2016': 0.16; 'async': 0.16; 'conn': 0.16; 'crud': 0.16; 'expert,': 0.16; 'googling': 0.16; 'ian.': 0.16; 'iterator': 0.16; 'larger,': 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; 'skip:n 70': 0.16; 'sqlite3': 0.16; 'wrote:': 0.16; 'attribute': 0.18; 'basically': 0.18; 'work,': 0.21; 'commands,': 0.22; 'trying': 0.22; 'am,': 0.23; 'fit': 0.23; 'bit': 0.23; 'seems': 0.23; 'wrote': 0.23; 'skip:l 40': 0.23; 'thanks,': 0.24; 'tried': 0.24; 'import': 0.24; 'examples': 0.24; 'header:In-Reply-To:1': 0.24; "doesn't": 0.26; 'header:X-Complaints-To:1': 0.26; 'define': 0.27; 'function': 0.28; "skip:' 10": 0.28; 'looks': 0.29; 'blocking': 0.29; 'url:peps': 0.29; 'minimal': 0.30; 'probably': 0.31; 'skip:_ 10': 0.32; 'class': 0.33; 'url:python': 0.33; 'consist': 0.33; 'right?': 0.33; 'wrap': 0.33; 'tue,': 0.34; 'running': 0.34; 'that,': 0.34; 'on,': 0.35; 'could': 0.35; 'url:dev': 0.35; "isn't": 0.35; 'but': 0.36; 'should': 0.36; 'url:org': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'list.': 0.37; 'seem': 0.37; 'doing': 0.38; 'creation': 0.38; 'self': 0.38; 'does': 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'some': 0.40; 'future': 0.60; 'entire': 0.61; 'provide': 0.61; 'impact': 0.61; 'show': 0.62; 'here.': 0.62; 'times': 0.63; 'decided': 0.66; 'fact,': 0.67; '26,': 0.72; 'frank': 0.72; 'await': 0.76; 'arg1,': 0.84; 'isolate': 0.84; 'odd,': 0.84; 'working,': 0.84 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: 197.89.169.53 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.20+ 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:102169 "Ian Kelly" wrote in message news:CALwzidk-RBkB-vi6CgcEeoFHQrsoTFvqX9MqzDD=rnY5bOCRUg@mail.gmail.com... > On Tue, Jan 26, 2016 at 7:15 AM, Frank Millman wrote: > > > > If I return the cursor, I can iterate over it, but isn't this a blocking > > operation? As far as I know, the DB adaptor will only actually retrieve > > the > > row when requested. > > > > If I am right, I should call fetchall() while inside get_rows(), and > > return > > all the rows as a list. > > > > You probably want an asynchronous iterator here. If the cursor doesn't > provide that, then you can wrap it in one. In fact, this is basically > one of the examples in the PEP: > https://www.python.org/dev/peps/pep-0492/#example-1 > Thanks, Ian. I had a look, and it does seem to fit the bill, but I could not get it to work, and I am running out of time. Specifically, I tried to get it working with the sqlite3 cursor. I am no expert, but after some googling I tried this - import sqlite3 conn = sqlite3.connect('/sqlite_db') cur = conn.cursor() async def __aiter__(self): return self async def __anext__(self): loop = asyncio.get_event_loop() return await loop.run_in_executor(None, self.__next__) import types cur.__aiter__ = types.MethodType( __aiter__, cur ) cur.__anext__ = types.MethodType( __anext__, cur ) It failed with this exception - AttributeError: 'sqlite3.Cursor' object has no attribute '__aiter__' I think this is what happens if a class uses 'slots' to define its attributes - it will not permit the creation of a new one. Anyway, moving on, I decided to change tack. Up to now I have been trying to isolate the function where I actually communicate with the database, and wrap that in a Future with 'run_in_executor'. In practice, the vast majority of my interactions with the database consist of very small CRUD commands, and will have minimal impact on response times even if they block. So I decided to focus on a couple of functions which are larger, and try to wrap the entire function in a Future with 'run_in_executor'. It seems to be working, but it looks a bit odd, so I will show what I am doing and ask for feedback. Assume a slow function - async def slow_function(arg1, arg2): [do stuff] It now looks like this - async def slow_function(arg1, arg2): loop = asyncio.get_event_loop() await loop.run_in_executor(None, slow_function_1, arg1, arg2) def slow_function_1(self, arg1, arg2): loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) loop.run_until_complete(slow_function_2(arg1, arg2)) async slow_function_2(arg1, arg2): [do stuff] Does this look right? Frank