Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #102089
| Path | csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | "Frank Millman" <frank@chagford.com> |
| Newsgroups | comp.lang.python |
| Subject | Re: Question about asyncio and blocking operations |
| Date | Mon, 25 Jan 2016 11:04:58 +0200 |
| Lines | 62 |
| Message-ID | <mailman.217.1453713006.15297.python-list@python.org> (permalink) |
| References | <n8038j$575$1@ger.gmane.org> <CALwzidnGOgpx+CpMVBA8vpEFuq4-BwMVS0gZ3ShB0oWZi0Bw+Q@mail.gmail.com> |
| 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 nE9lJc0QUfkC9eOfvRSKvAKhoOcQ3KgipyL7KO7u9AFA== |
| 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.001 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'context': 0.05; 'subject:Question': 0.05; 'testing,': 0.05; '__init__': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'tasks,': 0.09; 'jan': 0.11; 'def': 0.13; 'value.': 0.15; '2016': 0.16; '23,': 0.16; 'async': 0.16; 'background,': 0.16; 'coroutines': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'skip:n 70': 0.16; 'stuff)': 0.16; 'wrote:': 0.16; 'library,': 0.18; 'typical': 0.18; 'skip:" 30': 0.20; 'skip:" 40': 0.20; 'file:': 0.22; 'function,': 0.22; 'am,': 0.23; 'seems': 0.23; 'wrote': 0.23; 'sat,': 0.23; 'slightly': 0.23; 'second': 0.24; 'tried': 0.24; 'header:In-Reply-To:1': 0.24; 'header:X-Complaints-To:1': 0.26; 'appreciated.': 0.27; 'error': 0.27; 'skip:t 40': 0.27; 'function': 0.28; 'this.': 0.28; "skip:' 10": 0.28; 'initial': 0.28; 'looks': 0.29; 'blocking': 0.29; 'closer': 0.29; 'such.': 0.29; 'thread,': 0.29; 'skip:g 30': 0.30; 'task': 0.30; 'getting': 0.33; 'run': 0.33; 'common': 0.33; 'recommended': 0.34; 'handle': 0.34; 'skip:d 20': 0.34; 'info': 0.34; 'gets': 0.35; 'programming.': 0.35; 'level': 0.35; 'but': 0.36; 'there': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'method': 0.37; 'turn': 0.37; 'received:org': 0.37; 'itself': 0.38; 'end': 0.39; 'why': 0.39; 'does': 0.39; 'to:addr:python.org': 0.40; 'back': 0.62; 'skip:n 10': 0.62; 'different': 0.63; "they're": 0.66; 'here': 0.66; 'future.': 0.67; 'frank': 0.72; 'further,': 0.72; 'await': 0.76; 'experiment': 0.84; 'iow': 0.84; 'returns.': 0.84; 'continue.': 0.91; 'suspended': 0.91; 'suspended.': 0.93 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| X-Gmane-NNTP-Posting-Host | 197.86.210.60 |
| In-Reply-To | <CALwzidnGOgpx+CpMVBA8vpEFuq4-BwMVS0gZ3ShB0oWZi0Bw+Q@mail.gmail.com> |
| 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 <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:102089 |
Show key headers only | View raw
"Ian Kelly" wrote in message
news:CALwzidnGOgpx+CpMVBA8vpEFuq4-BwMVS0gZ3ShB0oWZi0Bw+Q@mail.gmail.com...
>
> On Sat, Jan 23, 2016 at 7:38 AM, Frank Millman <frank@chagford.com> wrote:
> > Here is the difficulty. The recommended way to handle a blocking
> > operation
> > is to run it as task in a different thread, using run_in_executor().
> > This
> > method is a coroutine. An implication of this is that any method that
> > calls
> > it must also be a coroutine, so I end up with a chain of coroutines
> > stretching all the way back to the initial event that triggered it.
>
> 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 ... >
2. In the first function, calling 'run_in_executor' unblocks the main loop
so that it can continue with other tasks, but the function itself is
suspended until the blocking function returns. In the second function, I
cannot see how the function gets suspended. It looks as if the blocking
function will run in the background, and the main function will continue.
I would like to experiment with this further, but I would need to see the
broader context - IOW see the 'caller' of normal_function(), and see what it
does with the return value.
I feel I am getting closer to an 'aha' moment, but I am not there yet, so
all info is appreciated.
Frank
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Question about asyncio and blocking operations "Frank Millman" <frank@chagford.com> - 2016-01-25 11:04 +0200
csiph-web