[英]How does asyncio main thread manage running either the event loop and coroutines at the same time?
Firs of all, I apologize for my English.首先,我为我的英语道歉。 I am learning python asyncio module and I get confused at some point.
我正在学习 python 异步模块,我有时会感到困惑。 I learn from the doc that the main purpose of the event loop is to schedule coroutines for running, open newtwork I/O... A running coroutine can yield control back to the event loop by awaiting itself, so correct me if I am wrong, for the above to be possible, the event loop is needed to be always running on the main thread?
我从文档中了解到,事件循环的主要目的是安排协程运行,打开 newtwork I/O ... 正在运行的协程可以通过等待自身将控制权交还给事件循环,所以如果我错了,请纠正我,为了使上述成为可能,事件循环是否需要始终在主线程上运行? If so, how does asyncio manage running either the event loop and the coroutine at the same time on the same thread?
如果是这样,asyncio 如何管理在同一线程上同时运行事件循环和协程? Perhaps there is a mechanism to suspend the event loop and resume it when needed?
也许有一种机制可以暂停事件循环并在需要时恢复它? Or is the event loop has a dedicated thread (I am not sure)?
还是事件循环有一个专用线程(我不确定)?
An event loop is the thread that runs the coroutines and also waits on events to happen.事件循环是运行协程并等待事件发生的线程。
*------------*
^ |
| wait for next event
| |
| get coroutine that handles the event
| |
| resume coroutine
| |
| coroutine exits or enqueues a new event to wait on
| |
*------------*
An event can only be handled when the event loop gets back to the top of the loop and asks the OS for any events that need to be handled.只有当事件循环回到循环顶部并向操作系统询问需要处理的任何事件时,才能处理事件。 Events finishing do not interrupt the current coroutine.
事件完成不会中断当前协程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.