asyncio-redis icon indicating copy to clipboard operation
asyncio-redis copied to clipboard

The right way to share the returned connection.

Open tinylambda opened this issue 7 years ago • 0 comments

I want to know what's the right way to share the connection.

Create a class and add a classmethod,like this

`class MainHandler(tornado.web.RequestHandler): @classmethod async def redis_connection(cls): if not hasattr(cls, '_redis_connection'): cls._redis_connection = await asyncio_redis.Connection.create('127.0.0.1', 6379) return cls._redis_connection

async def get(self):
    print('get you!')
    connection: asyncio_redis.Connection = await MainHandler.redis_connection()
    # subscriber = await connection.start_subscribe()
    # await subscriber.subscribe(['chan1'])
    print(id(connection))
    try:
        hit = await connection.brpop(["zzz"], timeout=60)
    except asyncio_redis.TimeoutError as _:
        hit = None
    finally:
        connection.close()
    # hit = await subscriber.next_published()
    self.write("Hello, World, {}".format(hit))`

I found that the pubsub function wen wrong, two request need two messages be sent to the chan1 to return .

any suggestions?

tinylambda avatar Aug 14 '18 16:08 tinylambda