Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New command to toggle running the asyncio loop (similar to python -m asyncio) ! #42

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

lonetwin
Copy link
Owner

@lonetwin lonetwin commented Jul 28, 2022

pythonrc.py is now async capable !

Now, you can declare async def functions and await on them from the python prompt. This ability is enabled using the config.TOGGLE_ASYNCIO_LOOP_CMD (defaults to \A),

Details

This functionality is mostly borrowed from the stdlib implementation of asyncio.__main__. The essential idea being that on invoking the \A command:

  • a new asyncio loop is started in the main interpreter thread
  • a new (nested) interpreter session is launched in a separate thread (which shares the stdin/stdout/stderror since we don't do anything special beyond launching the new session
  • the asyncio loop will continue to run until \A is invoked again
    • at which point the loop is stopped ...but the nested interpreter is not automatically exited
    • when the nested interpreter is exited (for instance using Ctrl+d), you are landed back into the original instance of the interpreter
  • a subsequent \A will restart the thread (potentially, also restarting the nested interpreter, if that was exited)

image

Other changes:

  • Start using isort --profile black pythonrc.py
  • black-en pythonrc.py
  • mypy clean pythonrc.py
  • Edit history with cursor at the last line

@lonetwin lonetwin changed the title async New command to toggle running the asyncio loop (similar to python -m asyncio) ! Jul 28, 2022
Unlike the `asyncio.__main__` implementation, we won't quit the interpreter on
stopping the ioloop. Furthermore, since the original `.interact()` already
runs in an endless loop we don't need to start the nested REPL as a daemon
thread.
...and make it explicit which thread is exiting on a KeyboardInterrupt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant