【发布时间】:2020-10-24 00:45:49
【问题描述】:
我正在通过多种传输方式发送数据
for transport in transports:
transport.sendto(packet)
传输列表的创建方式如下:
for country, ip in countries:
if country != my_country:
t, _ = await self._loop.create_datagram_endpoint(
lambda: ClientProtocol(country), remote_addr=(ip, port)
)
transports.add(t) # type: ignore
Sendto 定义为:
def sendto(self, data, addr=None):
"""Send data to the transport.
This does not block; it buffers the data and arranges for it
to be sent out asynchronously.
addr is target socket address.
If addr is None use target address pointed on transport creation.
"""
让我感到困惑的是sendto 说不阻塞,但它没有标记为async 功能。如果我的主循环中没有等待并且我正在做大量的 CPU 计算,sendto 会被饿死吗?是否需要添加一些awaits 以确保始终发送数据?
谢谢!
【问题讨论】:
-
你可能根本不应该使用这个 API。引用docs,“传输和协议由低级事件循环API使用......本质上,传输和协议应该只在库和框架中使用,永远不要在高级异步应用程序。”
-
不幸的是,这个特殊的应用程序对延迟非常敏感,并且有兴趣使用最有效的可用 API。
标签: python python-3.x async-await python-asyncio