我有一个使用协程方法的事件循环asyncio
。
我热衷于使用uvloop寻找以下示例的等效项。
这是一个简单的asyncio
事件循环示例:
import asyncio
async def read(**kwargs):
oid = kwargs.get('oid', '0.0.0.0.0.0')
time = kwargs.get('time', 1)
try:
print('start: ' + oid)
except Exception as exc:
print(exc)
finally:
await asyncio.sleep(time)
print('terminate: ' + oid)
def event_loop(configs):
loop = asyncio.get_event_loop()
for conf in configs:
asyncio.ensure_future(read(oid=conf['oid'], time=conf['time']))
return loop
if __name__ == '__main__':
snmp_configurations = [
{'time': 5, 'oid': '1.3.6.3.2.4'},
{'time': 6, 'oid': '1.3.6.3.5.8'},
] # TODO :: DUMMY
loop = event_loop(snmp_configurations)
try:
loop.run_forever()
except KeyboardInterrupt:
pass
finally:
print("Closing Loop")
loop.close()
问题:
如何使用uvloop改造上述代码段?
以下更改对于使用具有更高性能的uvloop是否正确?
import uvloop def event_loop(configs): asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) # TODO :: uvloop. loop = asyncio.get_event_loop() ` for conf in configs: asyncio.ensure_future(read(oid=conf['oid'], time=conf['time'])) return loop
[注意]:
- uvloop声称这使 asyncio 速度提高了 2-4 倍。