Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 18906

what's the equivalent of asyncio event loop with uvloop?

$
0
0

I have an event loop with a coroutine method using asyncio.

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

Here's a simple asyncio event loop example:

import asyncioasync 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 loopif __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 code snippet via uvloop?

  • Is the following changes correct for using [uvloop] 1 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.

Viewing all articles
Browse latest Browse all 18906


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>