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