python - Move to python3 asyncio from python2 gevent -
i'm looking on python3 asyncio , looks pretty awesome, i'm coming gevent. however, i'm still trying figure out how everything. let's i'm trying simple service connects redis queue , pops items it. things out of hand pretty quickly: i'll need context manager close redis connection when object gets destroyed, i'll need async redis driver, , i'll need catch sigint , sigterm signals.
import asyncio import asyncio_redis class agent(object): def __init__(self, name): print("hello, i'm %s" % name) self.name = name self.running = true # self.redis should become instance of asyncio_redis.connection def shutdown(self): self.running = false def __enter__(self): return self def __exit__(self): print("%s cleaned up" % name) self.redis.close() def loop(self): print("%s started looping" % name) while self.running: # msg should value self.redis.brpop(["queue"], 10) if msg: print "%s recv %s" % (name, msg) else: pass if __name__ == "__main__": loop = asyncio.get_event_loop() try: agent("agent1"): loop.add_signal_handler(signal.sigint, a.shutdown) loop.run_until_complete( a.loop() ) finally: loop.stop() loop.close()
can complete code , make run? thanks.
definitely have @ aioredis , asyncio-redis.
i've used both, i'm liking aioredis. has context managers (see docs examples) , incorporating python 3.5 syntax.
Comments
Post a Comment