python - `asyncio.new_event_loop` 有哪些用例?

标签 python asynchronous web python-asyncio

我意识到,如果循环尚不存在,get_event_loop 就会调用 new_event_loop - 我想知道是否有 new_event_loop 的原因可能会在典型的 get_event_loop 之外或代替典型的 get_event_loop 被调用。

似乎new_event_loop可用于:

  1. 单个应用程序中有多个事件循环。
  2. 在一个事件循环被杀死后创建一个新的事件循环。

不过,我想不出执行其中任何一个的充分理由。你们是否遇到过显式调用 new_event_loop 的用例?

最佳答案

显式事件循环创建主要用于两种情况:

  • 您需要在主线程之外运行事件循环。这是医生所说的:

The default policy defines context as the current thread, and manages an event loop per thread that interacts with asyncio. If the current thread doesn’t already have an event loop associated with it, the default policy’s get_event_loop() method creates one when called from the main thread, but raises RuntimeError otherwise

  • 您希望在单个应用程序中使用自定义策略(例如,支持同一应用程序针对不同平台的不同实现)。您可以阅读有关政策here .

基本上,如果您想更改默认事件循环类型,则需要策略。

关于python - `asyncio.new_event_loop` 有哪些用例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59848036/

相关文章:

c# - 除非调用线程空闲(方法已完成),否则不会调用 WebRequest 回调

javascript - 使用 C# 修改 Javascript

iis-7 - 在ISAPI筛选器上调用LoadLibraryEx

python - sqlalchemy orm - 根据另一个表更改表中的列

python - 返回列表的最高值

python - 如何在 Windows 上安装 IMDbPY?

python - ImportError - 将 Falcon 应用程序部署到 AWS Elastic Beanstalk

javascript - 如果我动态加载(在它之前)jQuery,为什么 jQuery 无法执行加载的脚本?

javascript - 从 API 获取数据并在完成后继续

javascript - 将应用程序连接到 AWS 上的 RethinkDB 服务器