| 1 |
aioconsole |
| 2 |
========== |
| 3 |
|
| 4 |
|docs-badge| |cov-badge| |ci-badge| |version-badge| |pyversion-badge| |
| 5 |
|
| 6 |
|
| 7 |
Asynchronous console and interfaces for asyncio |
| 8 |
|
| 9 |
aioconsole_ provides: |
| 10 |
|
| 11 |
* asynchronous equivalents to `input`_, `print`_, `exec`_ and `code.interact`_ |
| 12 |
* an interactive loop running the asynchronous python console |
| 13 |
* a way to customize and run command line interface using `argparse`_ |
| 14 |
* `stream`_ support to serve interfaces instead of using standard streams |
| 15 |
* the ``apython`` script to access asyncio code at runtime without modifying the sources |
| 16 |
|
| 17 |
⚠️ Limitations |
| 18 |
-------------- |
| 19 |
|
| 20 |
Better alternative to ``aioconsole.ainput()`` |
| 21 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 22 |
|
| 23 |
A common use case for aioconsole is the async alternative to the builtin `input`_ function. |
| 24 |
However, aioconsole_ was written in 2015 and since then the powerful prompt-toolkit_ library has gained better asyncio support. |
| 25 |
The recommended way to `prompt in an asyncio application <https://python-prompt-toolkit.readthedocs.io/en/master/pages/asking_for_input.html#prompt-in-an-asyncio-application>`_ is now to use the `prompt-toolkit`_ library: |
| 26 |
|
| 27 |
.. code:: python3 |
| 28 |
|
| 29 |
from prompt_toolkit import PromptSession |
| 30 |
from prompt_toolkit.patch_stdout import patch_stdout |
| 31 |
|
| 32 |
async def my_coroutine(): |
| 33 |
session = PromptSession() |
| 34 |
while True: |
| 35 |
with patch_stdout(): |
| 36 |
result = await session.prompt_async("Say something: ") |
| 37 |
print(f"You said: {result}") |
| 38 |
|
| 39 |
|
| 40 |
Better python consoles with async support |
| 41 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 42 |
|
| 43 |
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: |
| 44 |
|
| 45 |
- `Embedding a ptpython console in an asyncio program <https://github.com/prompt-toolkit/ptpython/blob/master/examples/asyncio-python-embed.py>`_ |
| 46 |
- `Using the await syntax in an IPython console <https://ipython.readthedocs.io/en/stable/whatsnew/version7.html#autowait-asynchronous-repl>`_ |
| 47 |
|
| 48 |
|
| 49 |
Requirements |
| 50 |
------------ |
| 51 |
|
| 52 |
* Python \>= 3.8 |
| 53 |
|
| 54 |
|
| 55 |
Installation |
| 56 |
------------ |
| 57 |
|
| 58 |
aioconsole_ is available on PyPI_ and GitHub_. |
| 59 |
Both of the following commands install the ``aioconsole`` package |
| 60 |
and the ``apython`` script. |
| 61 |
|
| 62 |
.. code:: console |
| 63 |
|
| 64 |
$ pip3 install aioconsole # from PyPI |
| 65 |
$ python3 setup.py install # or from the sources |
| 66 |
$ apython -h |
| 67 |
usage: apython [-h] [--serve [HOST:] PORT] [--no-readline] |
| 68 |
[--banner BANNER] [--locals LOCALS] |
| 69 |
[-m MODULE | FILE] ... |
| 70 |
|
| 71 |
Run the given python file or module with a modified asyncio policy replacing |
| 72 |
the default event loop with an interactive loop. If no argument is given, it |
| 73 |
simply runs an asynchronous python console. |
| 74 |
|
| 75 |
positional arguments: |
| 76 |
FILE python file to run |
| 77 |
ARGS extra arguments |
| 78 |
|
| 79 |
optional arguments: |
| 80 |
-h, --help show this help message and exit |
| 81 |
--serve [HOST:] PORT, -s [HOST:] PORT |
| 82 |
serve a console on the given interface instead |
| 83 |
--no-readline force readline disabling |
| 84 |
--banner BANNER provide a custom banner |
| 85 |
--locals LOCALS provide custom locals as a dictionary |
| 86 |
-m MODULE run a python module |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
Simple usage |
| 91 |
------------ |
| 92 |
|
| 93 |
The following example demonstrates the use of ``await`` inside the console: |
| 94 |
|
| 95 |
.. code:: console |
| 96 |
|
| 97 |
$ apython |
| 98 |
Python 3.5.0 (default, Sep 7 2015, 14:12:03) |
| 99 |
[GCC 4.8.4] on linux |
| 100 |
Type "help", "copyright", "credits" or "license" for more information. |
| 101 |
--- |
| 102 |
This console is running in an asyncio event loop. |
| 103 |
It allows you to wait for coroutines using the 'await' syntax. |
| 104 |
Try: await asyncio.sleep(1, result=3, loop=loop) |
| 105 |
--- |
| 106 |
|
| 107 |
.. code:: python3 |
| 108 |
|
| 109 |
>>> await asyncio.sleep(1, result=3) |
| 110 |
# Wait one second... |
| 111 |
3 |
| 112 |
>>> |
| 113 |
|
| 114 |
|
| 115 |
Documentation |
| 116 |
------------- |
| 117 |
|
| 118 |
Find more examples in the documentation_ and the `example directory`_. |
| 119 |
|
| 120 |
|
| 121 |
Contact |
| 122 |
------- |
| 123 |
|
| 124 |
Vincent Michel: [email protected] |
| 125 |
|
| 126 |
.. _aioconsole: https://pypi.python.org/pypi/aioconsole |
| 127 |
.. _GitHub: https://github.com/vxgmichel/aioconsole |
| 128 |
.. _input: https://docs.python.org/3/library/functions.html#input |
| 129 |
.. _print: https://docs.python.org/3/library/functions.html#print |
| 130 |
.. _exec: https://docs.python.org/3/library/functions.html#exec |
| 131 |
.. _code.interact: https://docs.python.org/3/library/code.html#code.interact |
| 132 |
.. _argparse: https://docs.python.org/dev/library/argparse.html |
| 133 |
.. _stream: https://docs.python.org/3/library/asyncio-stream.html |
| 134 |
.. _example directory: https://github.com/vxgmichel/aioconsole/blob/main/example |
| 135 |
.. _documentation: http://aioconsole.readthedocs.io/ |
| 136 |
.. _PyPI: aioconsole_ |
| 137 |
.. _IPython: https://ipython.readthedocs.io |
| 138 |
.. _ptpython: https://github.com/prompt-toolkit/ptpython |
| 139 |
.. _prompt-toolkit: https://python-prompt-toolkit.readthedocs.io |
| 140 |
|
| 141 |
.. |docs-badge| image:: https://readthedocs.org/projects/aioconsole/badge/?version=latest |
| 142 |
:target: http://aioconsole.readthedocs.io/ |
| 143 |
:alt: |
| 144 |
|
| 145 |
.. |ci-badge| image:: https://github.com/vxgmichel/aioconsole/workflows/CI/badge.svg |
| 146 |
:target: https://github.com/vxgmichel/aioconsole/actions?query=branch%3Amain |
| 147 |
:alt: |
| 148 |
|
| 149 |
.. |cov-badge| image:: https://codecov.io/gh/vxgmichel/aioconsole/branch/main/graph/badge.svg |
| 150 |
:target: https://codecov.io/gh/vxgmichel/aioconsole |
| 151 |
:alt: |
| 152 |
|
| 153 |
.. |version-badge| image:: https://img.shields.io/pypi/v/aioconsole.svg |
| 154 |
:target: https://pypi.python.org/pypi/aioconsole |
| 155 |
:alt: |
| 156 |
|
| 157 |
.. |pyversion-badge| image:: https://img.shields.io/pypi/pyversions/aioconsole.svg |
| 158 |
:target: https://pypi.python.org/pypi/aioconsole |
| 159 |
:alt: |