Python 3.8 新特性:原生支持 asyncio REPL 交互模式
Python 3.8 引入了原生的 asyncio REPL 支持,允许开发者通过 `python -m asyncio` 命令直接在交互环境中使用 await 关键字执行异步代码,无需借助 IPython 或手动调用 asyncio.run()。本文详解该功能的启用方式、使用示例及与旧版本的区别,帮助开发者更高效地进行异步代码调试与测试。
前言
Python 3.8 发布背景与关注理由
近期我持续撰写关于 Python 3.8 新功能的系列文章,并在个人项目中提前试用该版本。之所以对 Python 3.8 保持高度关注,是因为在 Python 2 官方维护于 2020 年终止后,Python 3.8 成为最后一个主要版本。尽管 Python 3.9 的具体发布时间表尚未公布,但根据过往发布规律,预计要等到 Python 3.8.4 发布之后才会推出 Python 3.9.0,这大概会在 2020 年末。因此,未来两年的技术讨论焦点将集中在 Python 3.8 上。本周五(2019-05-31)将发布 3.8.0 beta 1,目前开发者们正加紧合并代码以赶上这一版本。接下来我将陆续介绍几个新合并的特性,今天首先探讨 asyncio REPL。
REPL 基础概念
REPL 机制与异步编程挑战
REPL 是 Read-Eval-Print Loop(读取-求值-打印循环)的缩写,这是一种简单且交互式的编程环境,其工作流程包含以下四个步骤:
- Read(读取):获取用户输入的指令。
- Eval(求值):对输入的指令进行计算或执行。
- Print(打印):输出求值后的结果。
- Loop(循环):重复上述过程,实现持续的交互操作。
REPL 对于学习新编程语言极具价值,开发者可以通过即时输出快速验证对代码逻辑的理解。CPython 内置了这样一个基础编程环境:
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'
然而,官方自带的 REPL 功能相对有限。经验丰富的 Python 开发者通常倾向于使用 IPython,我大部分文章中的代码示例均在 IPython 中执行。值得注意的是,从 IPython 7.0 版本开始,它已支持 Async REPL(异步 REPL):
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 asyncio.sleep(1)
...: return 'B'
...:
In [5]: await b()
Out[5]: 'B'
In [6]: asyncio.run(b())
Out[6]: 'B'
简而言之,在 IPython 中可以直接使用 await 关键字,而无需显式调用 `asyncio.run(b())`。这在官方默认 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 asyncio.sleep(1)
... return 'B'
...
>>> await b()
File "<stdin>", line 1
SyntaxError: 'await' outside function
需要明确的是,await 关键字仅在异步函数内部有效。
Python 3.8 原生 asyncio REPL 使用指南
Python 3.8 的 asyncio REPL 功能
好消息是,官方 REPL 也进行了更新,现在原生支持 asyncio REPL。具体实现细节可参考延伸阅读链接 1:
./python.exe -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 "asyncio.run()".
Type "help", "copyright", "credits" or "license" for more information.
>>> import asyncio
>>> async def b():
... await asyncio.sleep(1)
... return 'B'
...
>>> await b()
'B'
>>> async def c():
... await asyncio.sleep(1)
... return 'C'
...
>>> task = asyncio.create_task(c())
>>> await task
'C'
>>> await asyncio.sleep(1)
请注意,激活该 REPL 并非直接输入 `python`,而是需要使用命令 `python -m asyncio`。此外,`import asyncio` 会在激活 REPL 时自动执行,无需手动导入。
延伸阅读
在查看代码之前,不妨尝试思考如何实现这一功能 :yum:
https://github.com/python/cpython/pull/13472
总结
以上介绍了 Python 3.8 的新特性——asyncio REPL,希望对大家有所帮助。如有任何疑问,欢迎留言,小编会及时回复。感谢大家对本站的支持!如果您觉得本文对您有帮助,欢迎转载,请注明出处,谢谢!
本站声明
本文内容仅供参考,不代表本站观点。转载或引用请注明出处。如发现内容问题,请通过联系我们页面反馈。













