Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Ian Kelly Newsgroups: comp.lang.python Subject: Re: Question about asyncio and blocking operations Date: Wed, 27 Jan 2016 10:14:13 -0700 Lines: 39 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de rPCb9uiErPCTAO4hw9zdWQLeb8mb8BS5nJN5YS/fEWDA== 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; 'received:209.85.223': 0.03; 'none,': 0.05; 'subject:Question': 0.05; 'bug.': 0.07; 'cursor': 0.09; 'oh,': 0.09; 'assume': 0.11; 'jan': 0.11; 'exception': 0.13; 'def': 0.13; 'wed,': 0.15; '2016': 0.16; '9:15': 0.16; 'async': 0.16; 'executor': 0.16; 'iterable,': 0.16; 'iterator': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'swallows': 0.16; 'wrote:': 0.16; 'try:': 0.18; 'next,': 0.22; 'am,': 0.23; 'header:In-Reply-To:1': 0.24; "doesn't": 0.26; 'example': 0.26; 'message-id:@mail.gmail.com': 0.27; 'pep': 0.29; 'raise': 0.29; 'skip:_ 10': 0.32; 'class': 0.33; 'similar': 0.33; 'except': 0.34; 'received:google.com': 0.35; 'could': 0.35; 'received:209.85': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:209': 0.38; 'self': 0.38; 'sure': 0.39; 'skip:e 20': 0.39; 'to:addr:python.org': 0.40; 'skip:u 10': 0.61; 'avoid': 0.61; 'present.': 0.72; 'await': 0.76; 'to:name:python': 0.84; 'wrapper,': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=thSssa3fypz7ZAGTwhP3mldIwiKtbWXhQUUC4V8GcA8=; b=zvn9LzrlFPqx5POckANoinnzcBGlEYTEy6YtiNZcBcsAdmGc2CRAcBc6R6u2LMqKbK TE24iBREIifLOTBVd6b5KSIrCvVsDNwgoSmeienLzIc4An9V79ohlztQVK+lWSu6c75P oOQKgve2ef72NOGpZegsr1Ve9lq33etFwCx2le2DIvyJmAw8x9UXvKjLU1hES135gwOw bSWQ8KNdPA2y09y9tQ4YuDPtNncW7pQwua0XMEp4b6xH1SBbPSrznpr3z+VU3Umxh/2m iqgb7qGeMgv2ZkNIjeWhXj5/N3nGEqKWsvadW95dNNNUnuh65LUA2GmCAUs7ghE7CVKp Bc9w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:content-type; bh=thSssa3fypz7ZAGTwhP3mldIwiKtbWXhQUUC4V8GcA8=; b=SZgj5WW6t99ZIXJAMxXQJL4Dsil/eyMwRhdlyW1cmAH4AF0KGcW1Jvzm8ggJGHHDoD zwDl5H25dizBOBbgA7fBPwkZaSrP4aIuzGfceV2MvLeFmGoZ2xyNdYfveYx8KnplmLLM nMJG9V15B0G3kh2PANoxoGVjgG0p4UhcTrt4BCzIKcJTh8fwrhJuagwiDFOT/nOVRSlF tM1FH32JRvUz+YbDK2YeR4IQW4xYcfmVwn/WS9Bape6hAE70aknlIYLD1lj5FTBxCndo 31yOO27YG6B/2Lc4ux5rRYj9+aZctkQbsuUO67tlf6n8RtFyZVwjMKiFm+SOXHU+0A2P tKxQ== X-Gm-Message-State: AG10YORNtLiao5mnnghUEIEy3lZp7vXm7Y+bTSlQSpz9KfGHg+/8Ewxl6iR0HZnBN9l4p/M5PS3eziWyxJCyVw== X-Received: by 10.107.11.68 with SMTP id v65mr28776806ioi.188.1453914893170; Wed, 27 Jan 2016 09:14:53 -0800 (PST) In-Reply-To: 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:102164 On Wed, Jan 27, 2016 at 9:15 AM, Ian Kelly wrote: > class CursorWrapper: > > def __init__(self, cursor): > self._cursor = cursor > > async def __aiter__(self): > return self > > async def __anext__(self): > loop = asyncio.get_event_loop() > return await loop.run_in_executor(None, next, self._cursor) Oh, except you'd want to be sure to catch StopIteration and raise AsyncStopIteration in its place. This could also be generalized as an iterator wrapper, similar to the example in the PEP except using run_in_executor to actually avoid blocking. class AsyncIteratorWrapper: def __init__(self, iterable, loop=None, executor=None): self._iterator = iter(iterable) self._loop = loop or asyncio.get_event_loop() self._executor = executor async def __aiter__(self): return self async def __anext__(self): try: return await self._loop.run_in_executor( self._executor, next, self._iterator) except StopIteration: raise StopAsyncIteration Unfortunately this doesn't actually work at present. EventLoop.run_in_executor swallows the StopIteration exception and just returns None, which I assume is a bug.