管理与Python的redis连接(Managing connection to redis from Python)
我在我的python应用程序中使用
redis-py
来存储Redis数据库中的简单变量或变量列表,所以我认为最好每次需要保存或检索变量时创建一个到redis服务器的连接,因为这样做不经常进行,并且不希望有可能超时的永久连接。阅读一些基本教程后,我使用Redis类创建了连接,但尚未找到关闭连接的方式,因为这是我第一次使用Redis。 我不确定我是否正在使用最佳方法来管理连接,所以我想为此提供一些建议。 这就是我现在
set
或get
一个变量的方式: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'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 everytime I need to save or retrieve a variable as this is not done very often and don't want to have a permanent connection that might timeout.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
set
ting orget
ting 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.
Thanks for your advice.
原文:https://stackoverflow.com/questions/12967107
最新回答
是柬埔寨磅士卑(KampongSpeu)
相关问答
更多用于城市名称,国家查找的API和服务[已关闭](API & Service for City name, Country lookups [closed])
如何检索用户当前的城市名称?(How to retrieve user's current city name?)
如何从Google地图的纬度和经度坐标获取城市名称?(How to get city name from latitude and longitude coordinates in Google Maps?)
使用地理位置获取城市名称(Get city name using geolocation)
如何创建一个范围来订购城市名称?(How to create a scope to order cities name?)
Mapbox获取当前的国家/城市名称(Mapbox get country/city name currently in view)
JSON世界城市名单(JSON World City list)
我如何才能在android中获取当前的城市名称?(How can i get the current city name in android?)
如何使用geonames API获取城市名称?(How to use geonames API to get city name?)
如何用Android确定最近,最大的城市名称?(How to determine closest, largest city name with Android?)
相关文章
更多Redis Cookbook
redis安装-redis集群管理
python top project of 2013
Redis概述
redis主从从架构搭建-redis集群管理
redis 集群环境搭建-redis集群管理
redis新增集群节点-redis集群管理
redis sentinel(哨兵) 配置详解-redis集群管理
redis主从复制的原理-redis集群管理
最新问答
更多获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
如何通过引用返回对象?(How is returning an object by reference possible?)
矩阵如何存储在内存中?(How are matrices stored in memory?)
每个请求的Java新会话?(Java New Session For Each Request?)
css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
xcode语法颜色编码解释?(xcode syntax color coding explained?)
在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
Copyright ©2023 peixunduo.com All Rights Reserved.粤ICP备14003112号
本站部分内容来源于互联网,仅供学习和参考使用,请莫用于商业用途。如有侵犯你的版权,请联系我们(neng862121861#163.com),本站将尽快处理。谢谢合作!