【问题标题】:Tornado + aioredis: why are my redis calls blocking?Tornado + aioredis:为什么我的 redis 调用会阻塞?
【发布时间】:2019-01-20 07:50:53
【问题描述】:

我尝试在 Tornado 和 Redis 上构建一个具有两个 API 端点的简单系统:

  1. API 从 Redis 读取值,或等待该值存在(使用 BRPOP : value = yield from redis.brpop("test"))
  2. 写入此值的 API(使用 LPUSH : redis.lpush("test", "the value"))。

所以我希望能够以任何顺序调用这些 API。事实上,如果我调用 2. then 1.,它会按预期工作,对 1. 的调用会立即返回值。

问题是,如果我调用 1. 然后 2.,两个请求都会阻塞以永不返回。

同时,当请求阻塞时,我仍然可以直接在 Redis 中LPUSH/BRPOP,即使是同一个键。同样,我可以在 Tornado 中调用其他处理程序。所以我猜这个块既不在 Redis 也不在 Tornado 中,而是在我使用 aioredis 中?也许是异步循环?但我不明白我错在哪里。有什么建议吗?

感谢您的帮助。

这是我的代码:

import tornado.ioloop
import tornado.web
from tornado import web, gen
from tornado.options import options, define
import aioredis
import asyncio


class WaitValueHandler(tornado.web.RequestHandler):
    @asyncio.coroutine
    def get(self):
        redis = self.application.redis
        value = yield from redis.brpop("test")
        self.write("I received a value: %s" % value)


class WriteValueHandler(tornado.web.RequestHandler):
    @asyncio.coroutine
    def get(self):
        redis = self.application.redis
        res = yield from redis.lpush("test", "here is the value")
        self.write("Ok ")


class Application(tornado.web.Application):
    def __init__(self):
        tornado.ioloop.IOLoop.configure('tornado.platform.asyncio.AsyncIOMainLoop')

        handlers = [
            (r"/get", WaitValueHandler),
            (r"/put", WriteValueHandler)
        ]

        super().__init__(handlers, debug=True)

    def init_with_loop(self, loop):
        self.redis = loop.run_until_complete(
            aioredis.create_redis(('localhost', 6379), loop=loop)
        )

if __name__ == "__main__":
    application = Application()
    application.listen(8888)

    loop = asyncio.get_event_loop()
    application.init_with_loop(loop)
    loop.run_forever()

【问题讨论】:

    标签: python-3.x redis async-await tornado python-asyncio


    【解决方案1】:

    好的,我知道原因了,as the doc states:

    在共享模式下阻塞操作(如 blpop、brpop 或长时间运行的 LUA 脚本)会阻塞连接,从而可能导致整个程序出现故障。

    这种阻塞问题可以通过对此类操作使用独占连接来轻松解决:

    redis = await aioredis.create_redis_pool(
        ('localhost', 6379),
        minsize=1,
        maxsize=1)
    
    async def task():
       # Exclusive mode
       with await redis as r:
           await r.set('key', 'val')
    asyncio.ensure_future(task())
    asyncio.ensure_future(task())
    # Both tasks will first acquire connection.
    

    【讨论】:

      猜你喜欢
      • 2012-06-30
      • 1970-01-01
      • 2011-10-16
      • 2021-10-19
      • 1970-01-01
      • 2020-01-05
      • 2019-08-08
      • 2012-08-23
      • 1970-01-01
      相关资源
      最近更新 更多