Skip to main content

Asynchronous console and interfaces for asyncio

Project description

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: vxgmichel@gmail.com

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aioconsole-0.8.2.tar.gz (37.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aioconsole-0.8.2-py3-none-any.whl (31.5 kB view details)

Uploaded Python 3

File details

Details for the file aioconsole-0.8.2.tar.gz.

File metadata

  • Download URL: aioconsole-0.8.2.tar.gz
  • Upload date:
  • Size: 37.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for aioconsole-0.8.2.tar.gz
Algorithm Hash digest
SHA256 25cb5530f58f7ab431e9af84fbb5417178287b6c3300d5b1185e3b129a227cef
MD5 b1f49c48b6652fbc41e67b946879b83a
BLAKE2b-256 764a71f535c85991e18e1626429a283d4fc6720053f38211affa888809089ded

See more details on using hashes here.

File details

Details for the file aioconsole-0.8.2-py3-none-any.whl.

File metadata

  • Download URL: aioconsole-0.8.2-py3-none-any.whl
  • Upload date:
  • Size: 31.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for aioconsole-0.8.2-py3-none-any.whl
Algorithm Hash digest
SHA256 00f3fabd6de5df2fad635e1e6a13ebe5bb2456b83b31e881ae41bc5862fd6a68
MD5 45af67bd6d34927d7615f02111eec463
BLAKE2b-256 031004ef3313a07e9152a84ce197aa11586376478c167322141e9c79eaedc25b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page