简体   繁体   English

asyncio 主线程如何管理同时运行事件循环和协程?

[英]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)?还是事件循环有一个专用线程(我不确定)?

The event loop事件循环

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.

相关问题 在主线程运行的asyncio事件循环中运行无限循环 - run infinite loop in asyncio event loop running off the main thread Python队列链接运行asyncio协同程序的对象与主线程输入 - Python queue linking object running asyncio coroutines with main thread input 在异步事件循环中运行协程和线程:退出时出错 - Running coroutines and threads in asyncio event loop: Errors while exiting 在一些协程完成后添加一个协程,在一个已经运行的事件循环中(同一个循环,同一个线程) - Add a coroutine after some coroutines have finished, in an already running event loop (same loop, same thread) 在 dict 中管理 asyncio 协程 - Manage asyncio coroutines in dict asyncio是否支持从非主线程运行子进程? - Does asyncio support running a subprocess from a non-main thread? 如何在 Python asyncio 事件循环中手动启动和停止运行阻塞代码的线程? - How to manually start and stop a thread running blocking code within a Python asyncio event loop? 为什么asyncio.get_event_loop方法检查当前线程是否为主线程? - Why asyncio.get_event_loop method checks if the current thread is the main thread? 当主线程运行无限循环时,为什么要花很长时间在不同的线程中导入模块? - Why does it take a long time to import a module in a different thread when the main thread is running an infinite loop? 如何创建一个永远在其上运行滚动协同程序的事件循环? - How to create an event loop with rolling coroutines running on it forever?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM