pythonrc icon indicating copy to clipboard operation
pythonrc copied to clipboard

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

Open lonetwin opened this issue 3 years ago • 0 comments

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 avatar Jul 28 '22 18:07 lonetwin