Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #102092

Re: Question about asyncio and blocking operations

Path csiph.com!news.mixmin.net!newsreader4.netcologne.de!news.netcologne.de!fu-berlin.de!uni-berlin.de!not-for-mail
From Ian Kelly <ian.g.kelly@gmail.com>
Newsgroups comp.lang.python
Subject Re: Question about asyncio and blocking operations
Date Mon, 25 Jan 2016 08:45:12 -0700
Lines 59
Message-ID <mailman.219.1453736755.15297.python-list@python.org> (permalink)
References <n8038j$575$1@ger.gmane.org> <CALwzidnGOgpx+CpMVBA8vpEFuq4-BwMVS0gZ3ShB0oWZi0Bw+Q@mail.gmail.com> <n84obi$o5d$1@ger.gmane.org> <CALwzidkUqejrB_UqEK5ero8FkEh7rOwXS=6McbdX9E1QKiocdw@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de EZvXNdFD/tC8LyQn/S8EfQGWGceoAS9akbVDPp0j+wCA==
Return-Path <ian.g.kelly@gmail.com>
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; 'source,': 0.04; '"""': 0.05; 'subject:Question': 0.05; 'testing,': 0.05; 'bug.': 0.07; '__init__': 0.09; 'reminded': 0.09; 'python': 0.10; 'jan': 0.11; 'def': 0.13; '2016': 0.16; 'async': 0.16; 'coroutines': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'skip:n 70': 0.16; 'stuff)': 0.16; 'wrote:': 0.16; 'documented': 0.18; 'library,': 0.18; 'typical': 0.18; '>>>': 0.20; 'skip:" 30': 0.20; 'skip:" 40': 0.20; 'file:': 0.22; 'function,': 0.22; 'am,': 0.23; 'code,': 0.23; 'seems': 0.23; 'wrote': 0.23; 'slightly': 0.23; 'second': 0.24; 'tried': 0.24; 'plain': 0.24; 'header:In-Reply-To:1': 0.24; 'discussion': 0.24; 'mon,': 0.24; 'error': 0.27; 'skip:t 40': 0.27; 'wonder': 0.27; 'message-id:@mail.gmail.com': 0.27; 'function': 0.28; 'this.': 0.28; 'such.': 0.29; "i'm": 0.30; 'skip:g 30': 0.30; 'task': 0.30; 'says': 0.32; 'common': 0.33; 'wrap': 0.33; 'handle': 0.34; 'skip:d 20': 0.34; 'received:google.com': 0.35; 'programming.': 0.35; 'returning': 0.35; 'level': 0.35; 'should': 0.36; 'needed': 0.36; 'received:209.85': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'turn': 0.37; 'received:209.85.213': 0.37; 'starting': 0.37; 'received:209': 0.38; 'why': 0.39; 'test': 0.39; "didn't": 0.39; 'to:addr:python.org': 0.40; 'some': 0.40; "they're": 0.66; 'note:': 0.66; 'future.': 0.67; 'worth': 0.67; 'future,': 0.70; 'await': 0.76; 'to:name:python': 0.84; 'this;': 0.93
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=40vnhke2zeXFkASNn7edL1EXal6WCPA9WyycyyVVnhU=; b=Ta5d0GIMRStMM5JLqNtCPlxvAva9PzBy82UT/1GeQGeRdPm30RDa8fg2NnghVY5oMd Q+XVPseYjP4obHmtafH8Be7QIuo6NacLIrAnAtBmXeOT6xwCVQ0UZ7GXT51qGHJaUYKf 9VUSpJ552o6RirnmjgHlXF9uNu571ucyjoGIae7+ULTDK4bvtOHzfN+F2W1JdhOvgzoi sYzer+c0TlxUMF3WXd6bPVxwhSnnqlpEEKRab3j/1hrwPvgMjzndYf94B6IpErck7OY6 FXEF7mlR+VmnfRPpMJuxCrH1KrTIGIz3c6iuII0fq2TBqWupnV/yxOlTpnghRzTEe8q6 nNzg==
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=40vnhke2zeXFkASNn7edL1EXal6WCPA9WyycyyVVnhU=; b=F/RNBTyo+l3jvQp9/VoVklsvShvz8txjoS7LOMNICKRoIvscdETLiEDUA5lNykQvrb VewEt5bHYeNbH5/p/9cg3FAnTNRAshaO7D7hDhnJYQWbPK25LFID1oCMDSF7lDPG3O66 5mjRii2GC2ZqGPOcGPg7fiziWKePCsVS8ERYpUHQImYCwrrlx0+nAPrlo0bSmWaIiKDv gibpw5CkVVhianhzqvkq2Mb9+qG6vktF2eZevwYsJe8yyjfyZQEC+sAhDDcMFseHkWuS iVra21tXSsuWekiOscRIwjF04/uSj2ZI3/1k1RMUpDBLDdIKbh/4RlEHfXlctJPwlyKd 1WGg==
X-Gm-Message-State AG10YOQGl1dctcgg8VTwN00REyEzdsIVDJNqVgX6G4IXWxrDnGR6tzBh2D0ShAbZFiqbEIA1RomPwBqf2blsMQ==
X-Received by 10.50.131.201 with SMTP id oo9mr17632611igb.68.1453736752073; Mon, 25 Jan 2016 07:45:52 -0800 (PST)
In-Reply-To <CALwzidkUqejrB_UqEK5ero8FkEh7rOwXS=6McbdX9E1QKiocdw@mail.gmail.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
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:102092

Show key headers only | View raw


On Mon, Jan 25, 2016 at 8:32 AM, Ian Kelly <ian.g.kelly@gmail.com> wrote:
>
> On Jan 25, 2016 2:04 AM, "Frank Millman" <frank@chagford.com> wrote:
>>
>> "Ian Kelly"  wrote in message
>> news:CALwzidnGOgpx+CpMVBA8vpEFuq4-BwMVS0gZ3ShB0oWZi0Bw+Q@mail.gmail.com...
>>>
>>> This seems to be a common misapprehension about asyncio programming.
>>> While coroutines are the focus of the library, they're based on
>>> futures, and so by working at a slightly lower level you can also
>>> handle them as such. So  while this would be the typical way to use
>>> run_in_executor:
>>>
>>> async def my_coroutine(stuff):
>>>     value = await get_event_loop().run_in_executor(None,
>>> blocking_function, stuff)
>>>     result = await do_something_else_with(value)
>>>     return result
>>>
>>> This is also a perfectly valid way to use it:
>>>
>>> def normal_function(stuff):
>>>     loop = get_event_loop()
>>>     coro = loop.run_in_executor(None, blocking_function, stuff)
>>>     task = loop.create_task(coro)
>>>     task.add_done_callback(do_something_else)
>>>     return task
>>
>>
>> I am struggling to get my head around this.
>>
>> 1. In the second function, AFAICT coro is already a future. Why is it
>> necessary to turn it into a task? In fact when I tried that in my testing, I
>> got an assertion error -
>>
>> File: "C:\Python35\lib\asyncio\base_events.py", line 211, in create_task
>>    task = tasks.Task(coro, loop=self)
>> File: "C:\Python35\lib\asyncio\tasks.py", line 70, in __init__
>>    assert coroutines.iscoroutine(coro), repr(coro)
>> AssertionError: <Future pending ... >
>
> I didn't test this; it was based on the documentation, which says that
> run_in_executor is a coroutine. Looking at the source, it's actually a
> function that returns a future, so this may be a documentation bug.

And now I'm reminded of this note in the asyncio docs:

"""
Note: In this documentation, some methods are documented as
coroutines, even if they are plain Python functions returning a
Future. This is intentional to have a freedom of tweaking the
implementation of these functions in the future. If such a function is
needed to be used in a callback-style code, wrap its result with
ensure_future().
"""

IMO such methods should simply be documented as awaitables, not
coroutines. I wonder if that's already settled, or if it's worth
starting a discussion around.

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Re: Question about asyncio and blocking operations Ian Kelly <ian.g.kelly@gmail.com> - 2016-01-25 08:45 -0700
  Re: Question about asyncio and blocking operations Rustom Mody <rustompmody@gmail.com> - 2016-01-25 22:01 -0800
    Re: Question about asyncio and blocking operations Marko Rauhamaa <marko@pacujo.net> - 2016-01-26 09:23 +0200
      Re: Question about asyncio and blocking operations Paul Rubin <no.email@nospam.invalid> - 2016-01-25 23:53 -0800

csiph-web