<bdo id='DOMoT'></bdo><ul id='DOMoT'></ul>

  • <small id='DOMoT'></small><noframes id='DOMoT'>

      <tfoot id='DOMoT'></tfoot>
      1. <legend id='DOMoT'><style id='DOMoT'><dir id='DOMoT'><q id='DOMoT'></q></dir></style></legend>
      2. <i id='DOMoT'><tr id='DOMoT'><dt id='DOMoT'><q id='DOMoT'><span id='DOMoT'><b id='DOMoT'><form id='DOMoT'><ins id='DOMoT'></ins><ul id='DOMoT'></ul><sub id='DOMoT'></sub></form><legend id='DOMoT'></legend><bdo id='DOMoT'><pre id='DOMoT'><center id='DOMoT'></center></pre></bdo></b><th id='DOMoT'></th></span></q></dt></tr></i><div id='DOMoT'><tfoot id='DOMoT'></tfoot><dl id='DOMoT'><fieldset id='DOMoT'></fieldset></dl></div>

        从 Python 管理与 redis 的连接

        Managing connection to redis from Python(从 Python 管理与 redis 的连接)

        <legend id='CyvYX'><style id='CyvYX'><dir id='CyvYX'><q id='CyvYX'></q></dir></style></legend>
        <tfoot id='CyvYX'></tfoot>
          <tbody id='CyvYX'></tbody>

          • <small id='CyvYX'></small><noframes id='CyvYX'>

                <bdo id='CyvYX'></bdo><ul id='CyvYX'></ul>

                  <i id='CyvYX'><tr id='CyvYX'><dt id='CyvYX'><q id='CyvYX'><span id='CyvYX'><b id='CyvYX'><form id='CyvYX'><ins id='CyvYX'></ins><ul id='CyvYX'></ul><sub id='CyvYX'></sub></form><legend id='CyvYX'></legend><bdo id='CyvYX'><pre id='CyvYX'><center id='CyvYX'></center></pre></bdo></b><th id='CyvYX'></th></span></q></dt></tr></i><div id='CyvYX'><tfoot id='CyvYX'></tfoot><dl id='CyvYX'><fieldset id='CyvYX'></fieldset></dl></div>
                1. 本文介绍了从 Python 管理与 redis 的连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在我的 python 应用程序中使用 redis-py 将简单的变量或变量列表存储在 Redis 数据库中,所以我认为最好每创建一个到 redis 服务器的连接我需要保存或检索变量的时间,因为这不经常这样做,而且我不希望有一个超时的永久连接.

                  I'm using redis-py in my python application to store simple variables or lists of variables in a Redis database, so I thought it would be better to create a connection to the redis server every time I need to save or retrieve a variable as this is not done very often and I don't want to have a permanent connection that timeout.

                  在阅读了一些基础教程后,我使用 Redis 类创建了连接,但还没有找到关闭连接的方法,因为这是我第一次使用 Redis.我不确定我是否使用了管理连接的最佳方法,所以我想对此提出一些建议.这就是我现在 setting 或 getting 变量的方式:

                  After reading through some basic tutorials, I created the connections using the Redis class, but have not found a way to close the connection, as this is the first time I'm using Redis. I'm not sure if I'm using the best approach for managing the connections so I would like some advice for this. This is how I'm setting or getting a variable now:

                  import redis
                  
                  def getVariable(variable_name):
                      my_server = redis.Redis("10.0.0.1")
                      response = my_server.get(variable_name)
                      return response
                  
                  def setVariable(variable_name, variable_value):
                      my_server = redis.Redis("10.0.0.1")
                      my_server.set(variable_name, variable_value)
                  

                  我基本上使用此代码来存储上次连接时间或获取平均每秒对我的应用程序完成的请求以及类似的东西.

                  I basically use this code to store the last connection time or to get an average of requests per second done to my app and stuff like that.

                  感谢您的建议.

                  推荐答案

                  Python使用引用计数器机制来处理对象,所以在block结束时,my_server对象会自动销毁并关闭连接.您不需要显式关闭它.

                  Python uses a reference counter mechanism to deal with objects, so at the end of the blocks, the my_server object will be automatically destroyed and the connection closed. You do not need to close it explicitly.

                  现在这不是您应该管理 Redis 连接的方式.每次操作的连接/断开连接成本太高,因此保持连接打开要好得多.使用 redis-py 可以通过声明连接池来完成:

                  Now this is not how you are supposed to manage Redis connections. Connecting/disconnecting for each operation is too expensive, so it is much better to maintain the connection opened. With redis-py it can be done by declaring a pool of connections:

                  import redis
                  
                  POOL = redis.ConnectionPool(host='10.0.0.1', port=6379, db=0)
                  
                  def getVariable(variable_name):
                      my_server = redis.Redis(connection_pool=POOL)
                      response = my_server.get(variable_name)
                      return response
                  
                  def setVariable(variable_name, variable_value):
                      my_server = redis.Redis(connection_pool=POOL)
                      my_server.set(variable_name, variable_value)
                  

                  请注意,连接池管理大部分是自动的,并且在 redis-py 中完成.

                  Please note connection pool management is mostly automatic and done within redis-py.

                  这篇关于从 Python 管理与 redis 的连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Running .jl file from R or Python(从 R 或 Python 运行 .jl 文件)
                  Running Julia .jl file in python(在 python 中运行 Julia .jl 文件)
                  Using PIP in a Azure WebApp(在 Azure WebApp 中使用 PIP)
                  How to run python3.7 based flask web api on azure(如何在 azure 上运行基于 python3.7 的烧瓶 web api)
                  Azure Python Web App Internal Server Error(Azure Python Web 应用程序内部服务器错误)
                  Run python dlib library on azure app service(在 azure app 服务上运行 python dlib 库)

                  <small id='wHUQ7'></small><noframes id='wHUQ7'>

                    <bdo id='wHUQ7'></bdo><ul id='wHUQ7'></ul>
                      • <tfoot id='wHUQ7'></tfoot>

                        <i id='wHUQ7'><tr id='wHUQ7'><dt id='wHUQ7'><q id='wHUQ7'><span id='wHUQ7'><b id='wHUQ7'><form id='wHUQ7'><ins id='wHUQ7'></ins><ul id='wHUQ7'></ul><sub id='wHUQ7'></sub></form><legend id='wHUQ7'></legend><bdo id='wHUQ7'><pre id='wHUQ7'><center id='wHUQ7'></center></pre></bdo></b><th id='wHUQ7'></th></span></q></dt></tr></i><div id='wHUQ7'><tfoot id='wHUQ7'></tfoot><dl id='wHUQ7'><fieldset id='wHUQ7'></fieldset></dl></div>

                        1. <legend id='wHUQ7'><style id='wHUQ7'><dir id='wHUQ7'><q id='wHUQ7'></q></dir></style></legend>
                              <tbody id='wHUQ7'></tbody>