[New-bugs-announce] [issue22239] asyncio: nested event loop

Daniel Arbuckle report at bugs.python.org
Wed Aug 20 20:03:50 CEST 2014


New submission from Daniel Arbuckle:

It's occasionally necessary to invoke the asyncio event loop from code that was itself invoked within (although usually not directly by) the event loop.

For example, imagine you are writing a class that serves as a local proxy for a remote data structure. You can not make the __contains__ method of that class into a coroutine, because Python automatically converts the return value into a boolean. However, __contains__ must invoke coroutines in order to communicate over the network, and it must be invokable from within a coroutine to be at all useful.

If the event loop _run_once method were reentrant, addressing this problem would be simple. That primitive could be used to create a loop_until_complete function, which could be applied to the io tasks that __contains__ needs to invoke

So, making _run_once reentrant is one way of addressing this request.

Alternately, I've attached a decorator that sets aside some of the state of _run_once, runs a couroutine to completion in a nested event loop, restores the saved state, and returns the coroutine's result. This is merely a proof of concept, but it does work, at least in my experiments.

----------
components: asyncio
files: nested.py
messages: 225578
nosy: djarb, gvanrossum, haypo, yselivanov
priority: normal
severity: normal
status: open
title: asyncio: nested event loop
type: enhancement
Added file: http://bugs.python.org/file36422/nested.py

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue22239>
_______________________________________


More information about the New-bugs-announce mailing list