|  | 
| 3 | 3 | Running on top of the `asyncio` event loop | 
| 4 | 4 | ========================================== | 
| 5 | 5 | 
 | 
| 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:: | 
| 10 | 7 | 
 | 
| 11 |  | -.. code:: | 
|  | 8 | + New in prompt_toolkit 3.0. (In prompt_toolkit 2.0 this was possible using a | 
|  | 9 | + work-around). | 
| 12 | 10 | 
 | 
| 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. | 
| 14 | 13 | 
 | 
| 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: | 
| 28 | 16 | 
 | 
| 29 | 17 | .. code:: python | 
| 30 | 18 | 
 | 
| 31 |  | - from prompt_toolkit.eventloop import use_asyncio_event_loop | 
| 32 | 19 |  from prompt_toolkit.application import Application | 
| 33 | 20 | 
 | 
| 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 | + ) | 
| 47 | 26 | 
 | 
| 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) | 
| 52 | 29 | 
 | 
| 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()) | 
0 commit comments