[英]What is event_loop_policy and why is it needed in python asyncio?
The event loops documentation mentions event_loop_policy
but doesn't describe what it is and why this abstract layer is needed in detail. 事件循环文档提到了
event_loop_policy
但没有描述它是什么以及为什么需要详细介绍这个抽象层。 (the documentation even says one can customize this layer). (文档甚至说可以自定义这一层)。
In addition, help(asyncio.get_event_loop_policy())
is just saying... 另外,
help(asyncio.get_event_loop_policy())
只是说......
UNIX event loop policy with a watcher for child processes.
带有用于子进程的观察程序的UNIX事件循环策略。
Then, I come to be more confused. 然后,我变得更加困惑。 What is
watcher
? watcher
是什么? What are child processes
in event loop
? event loop
中的child processes
是什么?
Event loop policy is an object that is uses to create, set or get event loops. 事件循环政策的对象是使用创建,设置或获取事件循环。 For example, when you call
asyncio.new_event_loop()
it is policy who will determine concrete returned event loop's class. 例如,当您调用
asyncio.new_event_loop()
时,将确定具体返回的事件循环类的策略。
Policy is needed if you for some reason would want to change default event loop type. 如果由于某种原因想要更改默认事件循环类型,则需要策略。 Encapsulating logic of creating loops inside a separate replaceable (what is convenient) policy object is a strategy programming pattern .
在单独的可替换(方便的)策略对象中创建循环的封装逻辑是策略编程模式 。
help(asyncio.get_event_loop_policy())
gives you docs for concrete policy used in your OS, in your case for _UnixDefaultEventLoopPolicy . help(asyncio.get_event_loop_policy())
为您提供了操作系统中使用的具体策略的文档,例如_UnixDefaultEventLoopPolicy 。
By the link you can see how things implemented there, find what watcher is and read it's docs : 通过链接,您可以看到在那里实现的内容,找到观察者的内容并阅读它的文档 :
class SafeChildWatcher(BaseChildWatcher):
"""'Safe' child watcher implementation.
This implementation avoids disrupting other code spawning processes by
polling explicitly each process in the SIGCHLD handler instead of calling
os.waitpid(-1).
This is a safe solution but it has a significant overhead when handling a
big number of children (O(n) each time SIGCHLD is raised)
"""
As you can see it's pretty low-level, OS-specific stuff and you usually don't need it to use asyncio
. 正如你所看到的那样,它是非常低级的,特定于操作系统的东西,你通常不需要它来使用
asyncio
。
I think you may need investigate in policies only if you're going to write your event loops and/or policies that manages them. 我认为只有在您要编写事件循环和/或管理它们的策略时,您才需要调查策略。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.