使用 asyncio 和 aiohttp 的多个非阻塞任务
multiple nonblocking tasks using asyncio and aiohttp
我正在尝试使用 asyncio 和 aiohttp 执行多个非阻塞任务,但我认为我这样做的方式效率不高。我认为最好使用 await 而不是 yield。有人可以帮忙吗?
def_init__(self):
self.event_loop = asyncio.get_event_loop()
def run(self):
tasks = [
asyncio.ensure_future(self.subscribe()),
asyncio.ensure_future(self.getServer()),]
self.event_loop.run_until_complete(asyncio.gather(*tasks))
try:
self.event_loop.run_forever()
@asyncio.coroutine
def getServer(self):
server = yield from self.event_loop.create_server(handler, ip, port)
return server
@asyncio.coroutine
def sunbscribe(self):
while True:
yield from asyncio.sleep(10)
self.sendNotification(self.sub.recieve())
def sendNotification(msg):
# send message as a client
我必须收听服务器并订阅收听广播,并根据广播的消息 POST 到不同的服务器。
await 不会比 yield from 更有效率。它可能更像 pythonic,但是
async def foo():
await some_future
和
@asyncio.coroutine
def foo()
yield from some_future
大致相同。当然,就效率而言,它们非常接近。 Await 是使用与 yield from 非常相似的逻辑实现的。 (有一个额外的方法调用 await 涉及,但通常在噪音中丢失)
在效率方面,删除订阅方法中的显式睡眠和轮询似乎是此设计的主要目标。与其休眠一段固定的时间,不如得到一个未来,指示接收调用何时成功,并且只有 运行 在接收有数据时订阅任务。
根据 PEP 492:
await , similarly to yield from , suspends execution of read_data
coroutine until db.fetch awaitable completes and returns the result
data.
It uses the yield from implementation with an extra step of validating
its argument. await only accepts an awaitable , which can be one of:
所以我没有发现您的代码存在效率问题,因为它们使用相同的实现。
不过,我很奇怪为什么你 return server
却从不使用它。
我在你的代码中看到的主要设计错误是你同时使用了:
self.event_loop.run_until_complete(asyncio.gather(*tasks))
try:
self.event_loop.run_forever()
据我所知,您只需要 run_forever()
一些额外的提示:
在我使用 asyncio 的实现中,我通常会确保在出现错误时关闭循环,否则这可能会导致大量泄漏,具体取决于您的应用程序类型。
try:
loop.run_until_complete(asyncio.gather(*tasks))
finally: # close the loop no matter what or you leak FDs
loop.close()
我也使用 Uvloop 而不是内置的,根据基准测试,它更有效。
import uvloop
...
loop = uvloop.new_event_loop()
asyncio.set_event_loop(loop)
我正在尝试使用 asyncio 和 aiohttp 执行多个非阻塞任务,但我认为我这样做的方式效率不高。我认为最好使用 await 而不是 yield。有人可以帮忙吗?
def_init__(self):
self.event_loop = asyncio.get_event_loop()
def run(self):
tasks = [
asyncio.ensure_future(self.subscribe()),
asyncio.ensure_future(self.getServer()),]
self.event_loop.run_until_complete(asyncio.gather(*tasks))
try:
self.event_loop.run_forever()
@asyncio.coroutine
def getServer(self):
server = yield from self.event_loop.create_server(handler, ip, port)
return server
@asyncio.coroutine
def sunbscribe(self):
while True:
yield from asyncio.sleep(10)
self.sendNotification(self.sub.recieve())
def sendNotification(msg):
# send message as a client
我必须收听服务器并订阅收听广播,并根据广播的消息 POST 到不同的服务器。
await 不会比 yield from 更有效率。它可能更像 pythonic,但是
async def foo():
await some_future
和
@asyncio.coroutine
def foo()
yield from some_future
大致相同。当然,就效率而言,它们非常接近。 Await 是使用与 yield from 非常相似的逻辑实现的。 (有一个额外的方法调用 await 涉及,但通常在噪音中丢失)
在效率方面,删除订阅方法中的显式睡眠和轮询似乎是此设计的主要目标。与其休眠一段固定的时间,不如得到一个未来,指示接收调用何时成功,并且只有 运行 在接收有数据时订阅任务。
根据 PEP 492:
await , similarly to yield from , suspends execution of read_data coroutine until db.fetch awaitable completes and returns the result data.
It uses the yield from implementation with an extra step of validating its argument. await only accepts an awaitable , which can be one of:
所以我没有发现您的代码存在效率问题,因为它们使用相同的实现。
不过,我很奇怪为什么你 return server
却从不使用它。
我在你的代码中看到的主要设计错误是你同时使用了:
self.event_loop.run_until_complete(asyncio.gather(*tasks))
try:
self.event_loop.run_forever()
据我所知,您只需要 run_forever()
一些额外的提示:
在我使用 asyncio 的实现中,我通常会确保在出现错误时关闭循环,否则这可能会导致大量泄漏,具体取决于您的应用程序类型。
try:
loop.run_until_complete(asyncio.gather(*tasks))
finally: # close the loop no matter what or you leak FDs
loop.close()
我也使用 Uvloop 而不是内置的,根据基准测试,它更有效。
import uvloop
...
loop = uvloop.new_event_loop()
asyncio.set_event_loop(loop)