你如何*正确*从 Tornado 查询 Redis? [英] How do you *properly* query Redis from Tornado?

查看:51
本文介绍了你如何*正确*从 Tornado 查询 Redis?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇从 Tornado 查询 Redis(或任何与此相关的任何数据库)的推荐方法是什么.

I'm curious what the recommended method of querying Redis (or any DB for that matter) is from Tornado.

我见过一些例子,比如 https://gist.github.com/357306 但它们所有人似乎都在使用对 redis 的阻塞调用.

I've seen some examples like https://gist.github.com/357306 but they all appear to be using blocking calls to redis.

我的理解是,为了避免 Tornado 停止运行,我需要使用非阻塞数据库库,例如为 Twisted 开发的库.

My understanding is that to avoid grinding Tornado to a halt, I need to be using non-blocking DB libraries like the ones developed for Twisted.

我错了吗?这应该怎么做?

Am I wrong? How is this supposed to be done?

推荐答案

当涉及到阻止诸如 BLPOP 之类的命令或收听 Pub/Sub 频道时,您将需要一个异步客户端,例如 tornado-redis. 你可以从 此演示 以了解如何使用 tornado-redis 客户端开发一个简单的公共聊天应用程序.

When it comes to blocking commands like BLPOP or listening to a Pub/Sub channel you'll need an asynchronous client like tornado-redis. You may start with this demo to see how the tornado-redis client may be used to develope a simple public chat application.

但我建议将同步 redis-py 客户端与 hiredis 对于大多数其他情况.

But I would recommend using the synchronous redis-py client in conjunction with hiredis for most other cases.

异步客户端的主要优点是您的服务器可以在等待 Redis 服务器响应的同时处理传入的请求.但是,Redis 服务器速度非常快,在大多数情况下,在 Tornado 应用程序中设置异步回调的开销会增加请求处理的总时间,而不是等待 Redis 服务器响应所花费的时间.

The main advantage of asynchronous client is that your server can handle incoming requests while waiting for Redis server response. However, the Redis server is so fast that in most cases an overhead of setting up asynchronous callbacks in your Tornado application adds more to the total time of request processing then the time spent on waiting for Redis server response.

使用异步客户端,您可能会尝试同时向Redis服务器发送多个请求,但Redis服务器是单线程的(就像Tornado服务器一样),因此它会逐个响应这些请求- 一个,你几乎一无所获.而且,事实上,你不必同时向同一个 Redis 服务器发送多个 Redis 命令,只要有 MGET/MSET 之类的管道和命令即可.

Using an asynchronous client you may try to send multiple requests to the Redis server at the same time, but the Redis server is a single-threaded one (just like Tornado server), so it will answer to these requests one-by-one and you'll gain almost nothing. And, in fact, you don't have to send multiple Redis commands at the same time to the same Redis server as long as there are pipelines and commands like MGET/MSET.

当您使用多个 Redis 服务器实例时,异步客户端有一些优势,但我建议使用同步 (redis-py) 客户端和像 twemproxy这个(后者支持流水线和 MGET/MSET 命令).

An asynchronous client has some advantages when you use several Redis server instances, but I suggest using a synchronous (redis-py) client and a proxy like twemproxy or this one (the latter supports pipelining and MGET/MSET commands).

另外我建议在 Tornado 应用程序中使用 redis-py 客户端时不要使用连接池.只需为您的应用程序连接到的每个 Redis 数据库创建一个 Redis 对象实例.

Also I suggest not to use the connection pooling when using the redis-py client in Tornado applications. Just create a single Redis object instance for each Redis database your application connects to.

这篇关于你如何*正确*从 Tornado 查询 Redis?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆