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

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

阅读一些基本教程后,我使用Redis类创建了连接,但尚未找到关闭连接的方式,因为这是我第一次使用Redis。 我不确定我是否正在使用最佳方法来管理连接,所以我想为此提供一些建议。 这就是我现在setget一个变量的方式:

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 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.

Thanks for your advice.


原文:https://stackoverflow.com/questions/12967107
2023-09-14 18:09

最新回答

是柬埔寨磅士卑(KampongSpeu)

相关问答

更多

用于城市名称,国家查找的API和服务[已关闭](API & Service for City name, Country lookups [closed])

这个API看起来很全面: Geobytes.com免费的ajax城市API This API looks comprehensive: Geobytes.com free ajax cities API

如何检索用户当前的城市名称?(How to retrieve user's current city name?)

你要做的是设置一个CLLocationManager ,找到你当前的坐标。 使用当前坐标您需要使用MKReverseGeoCoder来查找您的位置。 - (void)viewDidLoad { // this creates the CCLocationManager that will find your current location CLLocationManager *locationManager = [[[CLLocationManager alloc] ini...

如何从Google地图的纬度和经度坐标获取城市名称?(How to get city name from latitude and longitude coordinates in Google Maps?)

您可以从Geocoder对象调用getFromLocation(double, double, int)方法。 它将返回具有方法getLocality()的Address对象的列表。 Geocoder gcd = new Geocoder(context, Locale.getDefault()); List<Address> addresses = gcd.getFromLocation(lat, lng, 1); if (addresses.size() > 0) { System....

使用地理位置获取城市名称(Get city name using geolocation)

您将使用Google API进行此类操作。 请注意,您必须包含谷歌地图库才能使用。 Google地理编码器返回很多地址组件,所以您必须对有哪个城市进行有根据的猜测。 “administrative_area_level_1”通常是你正在寻找的,但有时候是你所在的城市。 无论如何 - 有关谷歌响应类型的更多细节可以在这里和这里找到。 下面是应该做的诀窍的代码: <!DOCTYPE html> <html> <head> <meta name="viewport" content="initia...

如何创建一个范围来订购城市名称?(How to create a scope to order cities name?)

所以你应该只加入origin_city : scope :by_origin_city_name, -> { joins(:origin_city).order('cities.name asc') } So you should join only origin_city: scope :by_origin_city_name, -> { joins(:origin_city).order('cities.name asc') }

Mapbox获取当前的国家/城市名称(Mapbox get country/city name currently in view)

如果使用queryRenderedFeatures(),则可以获取当前视图中呈现的所有要素的数组。 如果你在法国的规模,它会返回一个country_label id的功能。 如果您放大巴黎,您可以使用place_label_city ID找到巴黎。 你也许可以解析数组来显示你想要的。 您可能必须传入一个遮罩层,例如queryRenderedFeatures(图层) If you use queryRenderedFeatures() you can get a array of all the f...

JSON世界城市名单(JSON World City list)

我发现的最好的自动填充城市列表服务是Geobytes: http : //www.geobytes.com/free-ajax-cities-jsonp-api.htm 您每天最多可以免费执行50,000个请求。 这足以满足您的需求吗? The best autocomplete city list service I've found is the one from Geobytes : http://www.geobytes.com/free-ajax-cities-jsonp-api.htm...

我如何才能在android中获取当前的城市名称?(How can i get the current city name in android?)

这一个是从当前经纬度获取位置名称,长 Geocoder gcd = new Geocoder(context, Locale.getDefault()); List<Address> addresses = gcd.getFromLocation(lat, lng, 1); if (addresses.size() > 0) System.out.println(addresses.get(0).getLocality()); This one is for getting locat...

如何使用geonames API获取城市名称?(How to use geonames API to get city name?)

当然,这完全取决于您想要执行的实际搜索。 假设你想找到以Lon开头的英国所有地点。 将执行此搜索的URL(例如,对于真实搜索可能会发生很大变化)是: http://api.geonames.org/search?name_startsWith=lon&country=GB&maxRows=10&username=demo 您可以在浏览器中弹出它并查看结果: <geonames style="MEDIUM"> <totalResultsCount>334</totalResultsCount> <...

如何用Android确定最近,最大的城市名称?(How to determine closest, largest city name with Android?)

你可以使用android.location.Geocoder包中可用的GeoCoder。 JavaDocs在这里是JavaDocs 示例代码 List<Address> list = geoCoder.getFromLocation(location .getLatitude(), location.getLongitude(), 1); if (list != null & list.size() > 0) { ...

相关文章

更多

Redis Cookbook

Two years since its initial release, Redis already ...

redis安装-redis集群管理

安装redis [root@master opt]# mkdir /opt/redis [root ...

python top project of 2013

Hi Pythonistas! 测试和调试 Testing &amp; Debuggi ...

Redis概述

什么是Redis Redis是Remote Dictionary Server的缩写, Redis是一 ...

redis主从从架构搭建-redis集群管理

主从从架构 [root@master redis-master-slave]# vim 6382/re ...

redis 集群环境搭建-redis集群管理

集群架构 (1)所有的redis节点彼此互联(PING-PONG机制),内部使用二进制协议优化传输速度 ...

redis新增集群节点-redis集群管理

新增一个节点6383,并启动 执行redis-trib.rb add-node命令添加节点 redi ...

redis sentinel(哨兵) 配置详解-redis集群管理

1. redis sentinel(哨兵) redis sentinel(哨兵)是对Redis系统的 ...

redis主从复制的原理-redis集群管理

复制的过程原理 1、 当从库和主库建立master-slave关系后,会向主数据库发送SYNC命令; ...

最新问答

更多

获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)

我用Google搜索了一个解决方案。 “EnumDisplayModeProvider”是我自己设置网站的各种模式的枚举。 public EnumDisplayModeProvider GetDisplayModeId() { foreach (var mode in DisplayModeProvider.Instance.Modes) if (mode.CanHandleContext(HttpContext)) {

如何通过引用返回对象?(How is returning an object by reference possible?)

这相对简单:在类的构造函数中,您可以分配内存,例如使用new 。 如果你制作一个对象的副本,你不是每次都分配新的内存,而是只复制指向原始内存块的指针,同时递增一个也存储在内存中的引用计数器,使得每个副本都是对象可以访问它。 如果引用计数降至零,则销毁对象将减少引用计数并仅释放分配的内存。 您只需要一个自定义复制构造函数和赋值运算符。 这基本上是共享指针的工作方式。 This is relatively easy: In the class' constructor, you allocate m

矩阵如何存储在内存中?(How are matrices stored in memory?)

正如它在“熵编码”中所说的那样,使用Z字形图案,与RLE一起使用,在许多情况下,RLE已经减小了尺寸。 但是,据我所知,DCT本身并没有给出稀疏矩阵。 但它通常会增强矩阵的熵。 这是compressen变得有损的点:输入矩阵用DCT传输,然后量化量化然后使用霍夫曼编码。 As it says in "Entropy coding" a zig-zag pattern is used, together with RLE which will already reduce size for man

每个请求的Java新会话?(Java New Session For Each Request?)

你是如何进行重定向的? 您是否事先调用了HttpServletResponse.encodeRedirectURL()? 在这里阅读javadoc 您可以使用它像response.sendRedirect(response.encodeRedirectURL(path)); The issue was with the path in the JSESSIONID cookie. I still can't figure out why it was being set to the tomca

css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)

我认为word-break ,如果你想在一个单词中打破行,你可以指定它,这样做可以解决问题: .column { word-break:break-all; } jsFiddle演示。 您可以在此处阅读有关word-break属性的更多信息。 I think word-break, with which you can specify if you want to break line within a word, will do the trick: .column { word-break

无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)

我认为您忘记在分类时间内缩放输入图像,如train_test.prototxt文件的第11行所示。 您可能应该在C ++代码中的某个位置乘以该因子,或者使用Caffe图层来缩放输入(请查看ELTWISE或POWER图层)。 编辑: 在评论中进行了一次对话之后,结果发现在classification.cpp文件中错误地删除了图像均值,而在原始训练/测试管道中没有减去图像均值。 I think you have forgotten to scale the input image during cl

xcode语法颜色编码解释?(xcode syntax color coding explained?)

转到: Xcode => Preferences => Fonts & Colors 您将看到每个语法高亮颜色旁边都有一个简短的解释。 Go to: Xcode => Preferences => Fonts & Colors You'll see that each syntax highlighting colour has a brief explanation next to it.

在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)

你考虑过第三方拼写检查吗? 您可以将在C#中开发的自定义WinForms控件插入访问数据库吗? VB6控件怎么样? 如果你能找到一个使用第三方库进行拼写检查的控件,那可能会有效。 Have you considered a third party spell checker? Can you insert a custom WinForms controls developed in C# into an access database? What about a VB6 control? If

从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)

我有同样的问题,因为我在远程服务器上有两个图像,我需要在每天的预定义时间复制到我的本地服务器,这是我能够提出的代码... try { if(@copy('url/to/source/image.ext', 'local/absolute/path/on/server/' . date("d-m-Y") . ".gif")) { } else { $errors = error_get_last(); throw new Exception($err

从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))

我不确定我完全明白你在说什么。 你能编辑你的帖子并包含你正在做的Subversion命令/操作的特定顺序吗? 最好使用命令行svn客户端,以便容易为其他人重现问题。 如果您只是想获取文件的旧副本(即使该文件不再存在),您可以使用如下命令: svn copy ${repo}/trunk/moduleA/file1@${rev} ${repo}/trunk/moduleB/file1 其中${repo}是您的存储库的URL, ${rev}是您想要的文件的版本。 这将恢复该文件的旧版本,包括最高版本