asyncio的哪个部分是并发的?想要实现细节。

2024-05-13 03:18:52 发布

您现在位置:Python中文网/ 问答频道 /正文

异步模块的说明如下:

This module provides infrastructure for writing single-threaded concurrent code using coroutines, multiplexing I/O access over sockets and other resources, running network clients and servers, and other related primitives.

我一直在阅读关于新的和非凡的asyncio python模块/包/whatever的文章。我知道有pythongil,因此asyncio非常适合GIL,因为(目的是)在单个线程上使用事件循环来管理事情。那么什么是并发的呢?看来I/O似乎是并发的。我相信I/O操作是由操作系统处理的。那么在asyncio的内部,它是否编写了一个利用操作系统提供的函数的并发C扩展呢?在


Tags: 模块andasyncioforcodethisthreadedconcurrent
1条回答
网友
1楼 · 发布于 2024-05-13 03:18:52

在asyncio中,单线程IO并发是通过组合许多概念来实现的:

future          -+    -+
                          |         |
generator  -> coroutine -+-> task -+-> base event loop -+-> selector event loop
                                                         |
select  -> selector                   +

然而,正如curio所证明的那样,在没有期货的情况下也有可能达到同样的目的:

^{pr2}$

链接

Standard library

Asyncio

Curio

相关问题 更多 >