Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #106735
| Path | csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Ian Kelly <ian.g.kelly@gmail.com> |
| Newsgroups | comp.lang.python |
| Subject | Re: (Python 3.5) Asyncio and an attempt to run loop.run_until_complete() from within a running loop |
| Date | Sat, 9 Apr 2016 09:14:29 -0600 |
| Lines | 99 |
| Message-ID | <mailman.120.1460214919.2253.python-list@python.org> (permalink) |
| References | <33e44698-2625-47c4-9595-00a8c79f27ad@googlegroups.com> <CALwzid=nr4FTtt-iQJEG8CwnpbXR-=QctCous0fi0YdC1BP_cA@mail.gmail.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8 |
| Content-Transfer-Encoding | quoted-printable |
| X-Trace | news.uni-berlin.de OIC1kBI+2RFndtNb2k5ITAkTW4UztYDusd2zkukDmLFg== |
| 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.003 |
| X-Spam-Evidence | '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'subject:Python': 0.05; 'discard': 0.05; 'none:': 0.05; 'executed': 0.07; 'cc:addr:python-list': 0.09; 'here?': 0.09; 'loop.': 0.09; 'thread': 0.10; 'def': 0.13; 'instead.': 0.15; '2016': 0.16; 'alexander': 0.16; 'async': 0.16; 'cc:name:python': 0.16; 'coroutines': 0.16; 'help?': 0.16; 'instantiate': 0.16; 'iterable)': 0.16; 'iterable,': 0.16; 'iteration': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:run': 0.16; 'thread.': 0.16; 'wrote:': 0.16; 'result,': 0.18; 'skip:l 30': 0.18; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'trying': 0.22; 'code.': 0.23; 'slightly': 0.23; 'this:': 0.23; 'tried': 0.24; 'header:In-Reply-To:1': 0.24; "doesn't": 0.26; '(which': 0.26; 'fri,': 0.27; 'message-id:@mail.gmail.com': 0.27; 'function': 0.28; 'this.': 0.28; "skip:' 10": 0.28; 'attempting': 0.29; 'blocking': 0.29; 'loop,': 0.29; 'thread,': 0.29; 'launch': 0.29; 'starts': 0.29; 'allows': 0.30; "i'm": 0.30; "we're": 0.30; 'code': 0.30; 'becomes': 0.30; 'call.': 0.30; 'another': 0.32; 'subject:) ': 0.32; 'run': 0.33; 'problem': 0.33; 'previous': 0.34; 'running': 0.34; 'received:google.com': 0.35; 'so,': 0.35; 'returning': 0.35; 'stable': 0.35; 'something': 0.35; 'expected': 0.35; "isn't": 0.35; 'but': 0.36; 'should': 0.36; 'instead': 0.36; 'url:org': 0.36; 'lines': 0.36; 'received:209.85': 0.36; 'totally': 0.36; 'urls': 0.36; 'pm,': 0.36; 'really': 0.37; 'there,': 0.37; 'received:209.85.213': 0.37; 'starting': 0.37; 'doing': 0.38; 'itself': 0.38; "won't": 0.38; 'received:209': 0.38; 'wrong': 0.38; 'mean': 0.38; 'end': 0.39; 'means': 0.39; 'why': 0.39; 'sure': 0.39; 'does': 0.39; 'subject:from': 0.39; 'enough': 0.39; 'future': 0.60; 'ever': 0.60; 'waiting': 0.60; 'your': 0.60; 'here.': 0.62; 'inline': 0.63; 'more': 0.63; 'different': 0.63; 'for:': 0.64; 'here': 0.66; 'results': 0.66; 'future,': 0.70; 'await': 0.76; 'miss': 0.77; 'url:show': 0.84; 'to:none': 0.91; 'different.': 0.91; 'do:': 0.91; 'besides,': 0.93; 'wait,': 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:cc :content-transfer-encoding; bh=PclLjBYlMUIB2hwoXvf/MGgZeWXJmnJYp+nxYHs2FR8=; b=rIskqQzlAJlQ6O4BAhVtP8MYThu5l3xFZoic1e2zU/gYKCFsijo/cGt9e3TJAhb5Ii pWhfVEOoB7Vn5pzwgdWEjEyDv4syq/teGux/pdYTnKtWv9J+uFDyRMbEJWrYCKHnuL7S dekc56rhopHY9Kp0fmvFYWJpmrAmvGjgJ9rzcJrpXsAxb1Mlv0vX0qkR/7vlihrp2ZCa cWi2ZkDNs0zqjOoT88AQXTRgOsT3oqw7IfWuUTMcB1TpCoSsx+Zvv3JTD5gAO2ItT42p QcYdwxlW7jhK7NUm/tjlFhNkzBAImjHyQRkUyogRpGP5He5ocv3Bk2G9NhYpy6lhO/09 5WdA== |
| 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:cc:content-transfer-encoding; bh=PclLjBYlMUIB2hwoXvf/MGgZeWXJmnJYp+nxYHs2FR8=; b=BXPnQDbZEF9EkCB2Q4xwdOmtyFYfG3BNxY2+0Dzlb2D2zNMmCY1ri11eSXRLM0Q8wn mLlkNSvNyRkUiHjIqRGtQx68iLADeKSGIMp9wrcWen9NBdec0b8PRwDlQ3TObWVyWGXB +TxaTqmCzIXu9Ds+uf18tjqIX1gAsJ0ykWJrvq50FEmGz0DkaQDQrm/59ObeUXdhZdn9 QbbDbiFlZu2RKWkvVifRYJ8y//f7z1SZCIg7mkfkKs5tXv1ki+H5XfpfJzDQO4Dl3Pf6 vzbYN+qonwypyf6ts4mbtNIjm00PpTkr7zTlVKpQWNZ29MFdQiHRPhYNkS4PiAN14o+K X0Fg== |
| X-Gm-Message-State | AD7BkJIQmUFWzqFCVyEHFGigHSAmdEeQLm1zQMITEtPx4rz4S3SnLZeotQPjU7ofdi/XfQtZjkP3uoCmdPmn4g== |
| X-Received | by 10.50.57.50 with SMTP id f18mr9562824igq.93.1460214908783; Sat, 09 Apr 2016 08:15:08 -0700 (PDT) |
| In-Reply-To | <33e44698-2625-47c4-9595-00a8c79f27ad@googlegroups.com> |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.21 |
| 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> |
| X-Mailman-Original-Message-ID | <CALwzid=nr4FTtt-iQJEG8CwnpbXR-=QctCous0fi0YdC1BP_cA@mail.gmail.com> |
| X-Mailman-Original-References | <33e44698-2625-47c4-9595-00a8c79f27ad@googlegroups.com> |
| Xref | csiph.com comp.lang.python:106735 |
Show key headers only | View raw
On Fri, Apr 8, 2016 at 5:54 PM, Alexander Myodov <amyodov@gmail.com> wrote:
> Hello.
>
> TLDR: how can I use something like loop.run_until_complete(coro), to execute a coroutine synchronously, while the loop is already running?
>
> More on this:
>
> I was trying to create an aio_map(coro, iterable) function (which would asynchronously launch a coroutine for each iteration over iterable, and collect the data; similary to gevent.pool.Group.imap() from another async world), but stuck while attempting to make it well both from outside the async event loop and from inside one - any help?
>
> My code is at http://paste.pound-python.org/show/EQvN2cSDp0xqXK56dUPy/ - and I stuck around lines 22-28, with the problem that loop.run_until_complete() cannot be executed when the loop is running already (raising "RuntimeError: Event loop is running."). Is it normal? and why is it so restrictive? And what can I do to wait for `coros` Future to be finished?
>
> I tried various mixes of loop.run_forever() and even loop._run_once() there, but was not able to create a stable working code. Am I doing something completely wrong here? Am I expected to create a totally new event loop for synchronously waiting for the Future, if the current event loop is running - and if so, won't the previous event loop miss any its events?
The code is short enough that it would be better to include it inline
in your email for posterity; this thread will be archived, but paste
URLs have a bad tendency of eventually disappearing.
> def aio_map(coro, iterable, loop=None):
> if loop is None:
> loop = asyncio.get_event_loop()
>
> async def wrapped_coro(coro, value):
> return await coro(value)
>
> coroutines = (wrapped_coro(coro, v) for v in iterable)
I'm not sure what value wrapped_coro adds here. Couldn't you just do:
coroutines = (coro(v) for v in interable)
Or even:
coroutines = map(coro, iterables)
> coros = asyncio.gather(*coroutines, return_exceptions=True, loop=loop)
>
> if not loop.is_running():
> loop.run_until_complete(coros)
> else: # problem starts here
> # If we run loop.run_until_complete(coros) as well,
> # we get 'RuntimeError: Event loop is running.'
Right, there's no need to run multiple event loops.
> asyncio.wait(coros)
This doesn't do anything. asyncio.wait is itself a coroutine, so all
this does is to instantiate the asyncio.wait coroutine and then
discard it without ever starting it.
Besides, you don't really want to do this. aio_map isn't a coroutine,
which means it's a synchronous call. In order for it to wait, it would
have to block the event loop thread, which means that the coroutines
it's waiting for would never finish!
This should get you the result that you're looking for:
def aio_map(coro, iterable, loop=None):
if loop is None:
loop = asyncio.get_event_loop()
coroutines = map(coro, iterable)
coros = asyncio.gather(*coroutines, return_exceptions=True, loop=loop)
if loop.is_running():
return coros
else:
return loop.run_until_complete(coros)
Note that this does something slightly different. Instead of returning
the *result* if we're running in the event loop, we're going to return
the *future* instead. This allows the *caller* to await the result, so
that you don't end up blocking the event thread.
This does mean that the pattern for calling aio_map from outside the
event loop is different from calling it inside the event loop. Your
main_loop coroutine becomes (note the addition of the "await"):
async def main_loop(loop):
results = list(await aio_map(fetch_aio, range(5)))
assert(results == [{'aio_arg': '0'}, {'aio_arg': '1'}, {'aio_arg':
'2'}, {'aio_arg': '3'}, {'aio_arg': '4'}]), results
print('Assert ok!')
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
(Python 3.5) Asyncio and an attempt to run loop.run_until_complete() from within a running loop Alexander Myodov <amyodov@gmail.com> - 2016-04-08 16:54 -0700 Re: (Python 3.5) Asyncio and an attempt to run loop.run_until_complete() from within a running loop "Frank Millman" <frank@chagford.com> - 2016-04-09 07:08 +0200 Re: (Python 3.5) Asyncio and an attempt to run loop.run_until_complete() from within a running loop Ian Kelly <ian.g.kelly@gmail.com> - 2016-04-09 09:14 -0600 Re: (Python 3.5) Asyncio and an attempt to run loop.run_until_complete() from within a running loop Ian Kelly <ian.g.kelly@gmail.com> - 2016-04-09 09:38 -0600
csiph-web