Skip to main content
 首页 » 编程设计

python之有什么理由在异步/等待上使用 Python 的基于生成器的协程吗

2024年10月17日9softidea

尽管其著名的“一种方法”理念,Python(从 3.5 版开始)支持两种编写协程的方法:

  1. enhanced generators (可能与 yield from )
  2. async and await .

假设我们只需要支持 >= 3.5 的 Python 版本。是否有必要使用旧的、基于生成器的协程?还是基于 async/await 的协程完全取代基于生成器的协程?

请您参考如下方法:

引用 Guido van Rossum himself :

[...] eventually we'll always be using async functions when coroutines are called for, dedicating generators once again to their pre-PEP-342 role of a particularly efficient way to implement iterators.