asyncio event loop equivalent with uvloop

好久不见. 提交于 2020-04-30 10:22:38

问题


I have an event loop with coroutine method using asyncio.

I enthusiast to looking for an equivalent of the following example using uvloop instead.

Here's a simple asyncio event loop example:

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()

Question:

  • How to reform the above snippet code by using uvloop?

  • Is the following change correct for using uvloop with more performance?

    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
    

[NOTE]:

  • uvloop claims that makes asyncio 2-4x faster.

回答1:


Just set event loop policy before you call asyncio.get_event_loop().

import asyncio
import uvloop
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())

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()

Yes, this code is correct. You can set event loop policy after imports.

import uvloop
import asyncio
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())  # TODO  :: uvloop.

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


来源:https://stackoverflow.com/questions/53130168/asyncio-event-loop-equivalent-with-uvloop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!