px
pypx

aioconsole

v0.8.2

Asynchronous console and interfaces for asyncio

31 KB Python 3.8+ py3-none-anyGNU General Public License v3 or later (GPLv3+)
Diff
$ uv add aioconsole

aioconsole

Asynchronous console and interfaces for asyncio

aioconsole_ provides:

  • asynchronous equivalents to input_, print_, exec_ and code.interact_
  • an interactive loop running the asynchronous python console
  • a way to customize and run command line interface using argparse_
  • stream_ support to serve interfaces instead of using standard streams
  • the apython script to access asyncio code at runtime without modifying the sources

⚠️ Limitations --------------

Better alternative to aioconsole.ainput()

A common use case for aioconsole is the async alternative to the builtin input_ function. However, aioconsole_ was written in 2015 and since then the powerful prompt-toolkit_ library has gained better asyncio support. The recommended way to prompt in an asyncio application is now to use the prompt-toolkit_ library:

from prompt_toolkit import PromptSession
from prompt_toolkit.patch_stdout import patch_stdout

async def my_coroutine():
   session = PromptSession()
   while True:
      with patch_stdout():
            result = await session.prompt_async("Say something: ")
      print(f"You said: {result}")

Better python consoles with async support

The python console exposed by aioconsole_ is quite limited compared to modern consoles such as IPython_ or ptpython_. Luckily, those projects gained greater asyncio support over the years. In particular, the following use cases overlap with aioconsole_ capabilities:

Requirements

  • Python \>= 3.8

Installation

aioconsole_ is available on PyPI_ and GitHub_. Both of the following commands install the aioconsole package and the apython script.

$ pip3 install aioconsole   # from PyPI
$ python3 setup.py install  # or from the sources
$ apython -h
usage: apython [-h] [--serve [HOST:] PORT] [--no-readline]
               [--banner BANNER] [--locals LOCALS]
               [-m MODULE | FILE] ...

Run the given python file or module with a modified asyncio policy replacing
the default event loop with an interactive loop. If no argument is given, it
simply runs an asynchronous python console.

positional arguments:
  FILE                  python file to run
  ARGS                  extra arguments

optional arguments:
  -h, --help            show this help message and exit
  --serve [HOST:] PORT, -s [HOST:] PORT
                        serve a console on the given interface instead
  --no-readline         force readline disabling
  --banner BANNER       provide a custom banner
  --locals LOCALS       provide custom locals as a dictionary
  -m MODULE             run a python module

Simple usage

The following example demonstrates the use of await inside the console:

$ apython
Python 3.5.0 (default, Sep 7 2015, 14:12:03)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
---
This console is running in an asyncio event loop.
It allows you to wait for coroutines using the 'await' syntax.
Try: await asyncio.sleep(1, result=3, loop=loop)
---
>>> await asyncio.sleep(1, result=3)
# Wait one second...
3
>>>

Documentation

Find more examples in the documentation_ and the example directory_.

Contact

Vincent Michel: [email protected]

Details

Version
0.8.2
License
GNU General Public License v3 or later (GPLv3+)
Python
>=3.8
Maintainer
Vincent Michel <[email protected]>

Release Cadence

1
releases in the past year
avg 105 days between releases

Maintainers