SoFunction
Updated on 2025-03-02

Asyncio REPL, the new feature of Python 3.8

Preface

I have been writing some articles about introducing new features of Python 3.8 recently, and I am also experiencing the new Python version in advance in my project. Why am I so interested in this Python 3.8? This is mainly because before 2020, when Python 2 stopped official maintenance, Python 3.8 was the last major version. Although the release schedule for Python 3.9 has not been announced yet, based on past experience, I think that Python 3.9.0 can only be released after Python 3.8.4 is released, which should have been at the end of 2020. So everyone's topic in the past two years will be Python 3.8. 3.8.0 beta 1 will be released this Friday (2019-05-31). In recent days, developers have been rushing to merge the code and catch the last bus of Python 3.8. I will share several new merged features in the past few days. Today I will talk about asyncio REPL

REPL

REPL is the abbreviation of Read-Eval-Print Loop, a simple, interactive programming environment:

  • Read. Obtain user input
  • Eval. Evaluate the input
  • Print. Print, output the result of the evaluation
  • Loop. Loop, you can repeat Read-Eval-Print continuously

REPL is very helpful for learning a new programming language. You can quickly verify whether your understanding is correct through output in this interactive environment. CPython comes with a programming environment like this:

 python
Python 3.7.1 (default, Dec 13 2018, 22:28:16)
[Clang 10.0.0 (clang-1000.11.45.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> def a():
...   return 'A'
...
>>> a()
'A'

However, the official environment features are very limited. Experienced Python developers usually use IPython. Most of the code in the articles I wrote is executed in IPython. Moreover, IPython supports Async REPL since 7.0:

ipython

defPython 3.7.1 (default, Dec 13 2018, 22:28:16)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.5.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: def a():
  ...:   return 'A'
  ...:
In [2]: a()
Out[2]: 'A'
In [3]: import asyncio
In [4]: async def b():
  ...:   await (1)
  ...:   return 'B'
  ...:
In [5]: await b()
Out[5]: 'B'

In [6]: (b())
Out[6]: 'B'

Simply put, you can use await directly in IPython without using (b()). This is not possible in the official REPL:

python

Python 3.7.1 (default, Dec 13 2018, 22:28:16)
[Clang 10.0.0 (clang-1000.11.45.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import asyncio
>>> async def b():
...   await (1)
...   return 'B'
...
>>> await b()
 File "<stdin>", line 1
SyntaxError: 'await' outside function

Yes, await can only be used in asynchronous functions.

asyncio REPL for Python 3.8

The good news is that the official REPL is also keeping pace with the times and supports asyncio REPL. For specific details, please see the extended reading link 1:

./ -m asyncio
asyncio REPL 3.8.0a4+ (heads/master:8cd5165ba0, May 27 2019, 22:28:15)
[Clang 10.0.0 (clang-1000.11.45.5)] on darwin
Use "await" directly instead of "()".
Type "help", "copyright", "credits" or "license" for more information.
>>> import asyncio
>>> async def b():
...   await (1)
...   return 'B'
...
>>> await b()
'B'
>>> async def c():
...   await (1)
...   return 'C'
...
>>> task = asyncio.create_task(c())
>>> await task
'C'
>>> await (1)

Note that Activating REPL is not to directly enter python, but to use python -m asyncio . In addition, the import asyncio is automatically input for you when Activating REPL.

Further reading

Don't look at the code first, see if you can implement this function :yum:

/python/cpython/pull/13472

Summarize

The above is the new feature of Python 3.8 asyncio REPL introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!
If you think this article is helpful to you, please reprint it. Please indicate the source, thank you!