【发布时间】:2017-04-29 18:22:07
【问题描述】:
目前可以通过提供在每个传入连接上触发的回调来启动异步服务器:
async def on_connection(reader, writer):
# this is invoked each time a new connection is made
pass
server = await asyncio.start_server(on_connection, host, port)
我想摆脱回调并改用async for,所以它看起来像这样:
async for reader, writer in my_start_server(host, port):
# this should be invoked each time a new connection is made
pass
不幸的是,这似乎并不容易:
async def my_start_server(host, port):
async def on_connection(reader, writer):
# here I have to somehow yield (reader, writer) tuple
# but if I do just `yield (reader, writer)`, it would
# make `on_connection` itself a generator, but would
# not affect `my_start_server`
pass
server = await asyncio.start_server(on_connection, host, port)
我曾想过在那里有一个带有__aiter__ 实现的类,但结果似乎过于复杂了很多。那么,这是唯一的方法吗,还是我错过了将异步回调转换为异步生成器的任何简单方法?
【问题讨论】:
-
通常,您希望能够同时处理多个客户端。
async for方法不允许这样做。 -
@Vincent,嗯,你似乎是对的 -
async for不会从服务器获取下一个连接,直到你完成处理当前连接。我希望async for具有“尽快消费”语义。
标签: python python-3.x asynchronous python-asyncio asynccallback