简体   繁体   English

redis get 函数返回无

[英]redis get function return None

I am working on a flask application that interacts with redis.我正在开发一个与 redis 交互的烧瓶应用程序。 This applciation is deployed on heroku, with a redis add on.此应用程序部署在 heroku 上,并添加了 redis。

When I am doing some testing with the interaction, I am not able to get the key value pair that I just set.当我对交互进行一些测试时,我无法获得我刚刚设置的键值对。 Instead, I always get None as a return type.相反,我总是将 None 作为返回类型。 Here is the example:这是示例:

   import Flask
   import redis


   app = Flask(__name__)
   redis_url = os.getenv('REDISTOGO_URL', 'redis://localhost:6379')
   redis = redis.from_url(redis_url)

   @app.route('/test')
   def test():
      redis.set("test", "{test1: test}")
      print redis.get("test")   # print None here
      return "what the freak"


  if __name__ == "__main__":
       app.run(host='0.0.0.0')

As shown above, the test route will print None, means the value is not set.如上图,测试路由会打印None,表示未设置该值。 I am confused.我很困惑。 When I test the server on my local browser it works, and when I tried interacting with redis using heroku python shell it works too.当我在本地浏览器上测试服务器时,它可以工作,当我尝试使用 heroku python shell 与 redis 交互时,它也可以工作。

testing with python shell:使用 python shell 进行测试:

heroku run python
from server import redis
redis.set('test', 'i am here')  # return True
redis.get('test') # return i am here

I am confused now.我现在很困惑。 How should I properly interact with redis using Flask?我应该如何使用 Flask 与 redis 正确交互?

Redis-py by default constructs a ConnectionPool client, and this is probably what the from_url helper function is doing. Redis-py 默认构造了一个ConnectionPool客户端,这可能就是from_url辅助函数所做的。 While Redis itself is single threaded, the commands from the connection pool have no guaranteed order of execution.虽然 Redis 本身是单线程的,但连接池中的命令没有保证的执行顺序。 For a single client, construct a redis.StrictRedis client directly, or pass through the param connection_pool=none .对于单个客户端,直接构造一个redis.StrictRedis客户端,或者通过参数connection_pool=none This is preferable for simple commands, low in number, as there is less connection management overhead.这对于数量较少的简单命令更可取,因为连接管理开销较少。 You can alternatively use a pipeline in the context of a connection pool to serialise a batch operation.您也可以在连接池的上下文中使用管道来序列化批处理操作。

https://redis-py.readthedocs.io/en/latest/#redis.ConnectionPool https://redis-py.readthedocs.io/en/latest/#redis.ConnectionPool

https://redis-py.readthedocs.io/en/latest/#redis.Redis.pipeline https://redis-py.readthedocs.io/en/latest/#redis.Redis.pipeline

I did more experiments on this.我在这方面做了更多的实验。 It seems there is an issue related to the delay.似乎存在与延迟相关的问题。 the below modification will make it work:以下修改将使其工作:

  @app.route('/test')
  def test():
      redis.set("test", "{test1: test}")
      time.sleep(5) # add the delay needed to the let the set finish
      print redis.get("test")   # print "{test1: test}" here
      return "now it works"

I read the documentation on redis, redis seems to be single threaded.我阅读了有关 redis 的文档,redis 似乎是单线程的。 So I am not sure why it will execute the get function call before the set function is done.所以我不确定为什么它会在 set 函数完成之前执行 get 函数调用。 Someone with more experience please post an explanation.请有经验的人发表解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM