Message225578
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. |
|
Date |
User |
Action |
Args |
2014-08-20 18:03:50 | djarb | set | recipients:
+ djarb, gvanrossum, vstinner, yselivanov |
2014-08-20 18:03:50 | djarb | set | messageid: <1408557830.66.0.604610139339.issue22239@psf.upfronthosting.co.za> |
2014-08-20 18:03:50 | djarb | link | issue22239 messages |
2014-08-20 18:03:50 | djarb | create | |
|