Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The right way to share the returned connection. #125

Open
tinylambda opened this issue Aug 14, 2018 · 0 comments
Open

The right way to share the returned connection. #125

tinylambda opened this issue Aug 14, 2018 · 0 comments

Comments

@tinylambda
Copy link

tinylambda commented Aug 14, 2018

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant