What?
Asyncio has a repl (python -m asyncio
). I would like to propose to provide functionality analogous to the stdlib’s code.interact()
that allows to programatically invoke this repl.
The current limitations?
The asyncio REPL originally introduced in Implement asyncio repl · Issue #81209 · python/cpython · GitHub can not be invoked programatically due to the way it is implemented. Specifically:
- All the asyncio repl code is implemented in
asyncio.__main__
, providing no (proper) way to import it - A significant chunk of it is written as top-level code directly under an
if __name__ == "__main__"
block - The code relies on global variables, so even if one were to import things from
asyncio.__main__
, quite a few not so nice tricks must be employed to get it running outside apython -m asyncio
call
Why?
The rationale is basically the same as for why code.interact
exists. It’s convenient to build custom REPLs, which, while not a super common feature, can be quite useful to implement things like Django’s shell.
All the functionality already exists in the stdlib, so this would merely surface it.
The proposal
- Add a new
asyncio.console
module, implementing the asyncio REPL (basically just move all the code over fromasyncio.__main__
) - Remove the current implementation’s reliance on global variable manipulation
- Expose that functionality via an
interact()
function, mimicking the interface ofcode.interact()
- Refactor
asyncio.__main__
code to simply invokeasyncio.console.interact()
In fact, I have already done just that here (minus documentation and such): add asyncio.console module · provinzkraut/cpython@fc2a561 · GitHub
Alternatives
Other repls
There are alternative Python repls, like IPython which could be used if such functionality is desired.
However, they are an external dependency, and since the stlib already implements all the functionality necessary for this, it seems logical to simply expose that.
Standalone implementation
Another way to achieve this would be a simple “backport” of the functionality provided in asyncio.__main__
, but as it would basically be a copy-paste without adding any significant functionality, I think the same reasoning as for pre-existing alternative repls applies.
Potential downsides
- Losing some git history for the implementation in
asyncio.__main__
- ?
If there’s interest in this, I’d of course be happy to propose a PEP and a PR with the implementation.
PS: Excuse the lack of links to the things I’m referencing, but it seems that as a new user I’m not allowed more than two of those in a post