Skip to content

Commit 72c8327

Browse files
Prompt_toolkit 3.0 documentation improvements.
1 parent ab073d8 commit 72c8327

File tree

3 files changed

+47
-42
lines changed

3 files changed

+47
-42
lines changed

docs/pages/advanced_topics/asyncio.rst

Lines changed: 15 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,52 +3,28 @@
33
Running on top of the `asyncio` event loop
44
==========================================
55

6-
Prompt_toolkit has a built-in event loop of its own. However, in modern
7-
applications, you probably want to use `asyncio
8-
<https://docs.python.org/3/library/asyncio.html>`_ for everything. With just
9-
one line of code, it is possible to run prompt_toolkit on top of asyncio:
6+
.. note::
107

11-
.. code::
8+
New in prompt_toolkit 3.0. (In prompt_toolkit 2.0 this was possible using a
9+
work-around).
1210

13-
from prompt_toolkit.eventloop import use_asyncio_event_loop
11+
Prompt_toolkit 3.0 uses asyncio natively. Calling ``Application.run()`` will
12+
automatically run the asyncio event loop.
1413

15-
use_asyncio_event_loop()
16-
17-
This will create an adaptor between the asyncio event loop and prompt_toolkit,
18-
and register it as the underlying event loop for the prompt_toolkit
19-
application.
20-
21-
When doing this, remember that prompt_toolkit still has its own implementation
22-
of futures (and coroutines). A prompt_toolkit `Future` needs to be converted to
23-
an asyncio `Future` for use in an asyncio context, like asyncio's
24-
``run_until_complete``. The cleanest way is to call
25-
:meth:`~prompt_toolkit.eventloop.Future.to_asyncio_future`.
26-
27-
So,the typical boilerplace for an asyncio application looks like this:
14+
If however you want to run a prompt_toolkit ``Application`` within an asyncio
15+
environment, you have to call the ``prompt_async`` method, like this:
2816

2917
.. code:: python
3018
31-
from prompt_toolkit.eventloop import use_asyncio_event_loop
3219
from prompt_toolkit.application import Application
3320
34-
# Tell prompt_toolkit to use asyncio for the event loop.
35-
use_asyncio_event_loop()
36-
37-
# Define application.
38-
application = Application(
39-
...
40-
)
41-
42-
# Run the application, and wait for it to finish.
43-
asyncio.get_event_loop().run_until_complete(
44-
application.run_async().to_asyncio_future())
45-
46-
.. warning::
21+
async def main():
22+
# Define application.
23+
application = Application(
24+
...
25+
)
4726
48-
If you want to use coroutines in your application, then using asyncio is
49-
the preferred way. It's better to avoid the built-in coroutines, because
50-
they make debugging the application much more difficult. Unless of course
51-
Python 2 support is still required.
27+
result = await application.run_async()
28+
print(result)
5229
53-
At some point, when we drop Python 2 support, prompt_toolkit will probably
54-
use asyncio natively.
30+
asyncio.get_event_loop().run_until_complete(main())

docs/pages/reference.rst

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,8 @@ Eventloop
227227
.. automodule:: prompt_toolkit.eventloop
228228
:members: EventLoop, get_traceback_from_context, From, Return,
229229
ensure_future, create_event_loop, create_asyncio_event_loop,
230-
use_asyncio_event_loop, get_event_loop, set_event_loop,
231-
run_in_executor, call_from_executor, run_until_complete, Future,
232-
InvalidStateError
230+
get_event_loop, set_event_loop, run_in_executor, call_from_executor,
231+
run_until_complete, Future, InvalidStateError
233232

234233
.. automodule:: prompt_toolkit.eventloop.posix
235234
:members:

docs/pages/upgrading/3.0.rst

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,36 @@ There are some changes to the eventloop API:
5858
+-----------------------------------+--------------------------------------+
5959

6060

61+
Running on top of asyncio
62+
-------------------------
63+
64+
For 2.0, you had tell prompt_toolkit to run on top of the asyncio event loop.
65+
Now it's the default. So, you can simply remove the following two lines:
66+
67+
.. code::
68+
69+
from prompt_toolkit.eventloop.defaults import use_asyncio_event_loop
70+
use_asyncio_event_loop()
71+
72+
There is a few little breaking changes though. The following:
73+
74+
.. code::
75+
76+
# For 2.0
77+
result = await PromptSession().prompt('Say something: ', async_=True)
78+
79+
has to be changed into:
80+
81+
.. code::
82+
83+
# For 3.0
84+
result = await PromptSession().prompt_async('Say something: ')
85+
86+
Further, it's impossible to call the `prompt()` function within an asyncio
87+
application (within a coroutine), because it will try to run the event loop
88+
again. In that case, always use `prompt_async()`.
89+
90+
6191
Changes to the the dialog functions
6292
-----------------------------------
6393

0 commit comments

Comments
 (0)