Managing connection to redis from Python

2024/11/20 12:21:40

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.

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 redisdef getVariable(variable_name):my_server = redis.Redis("10.0.0.1")response = my_server.get(variable_name)return responsedef 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.

Answer

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.

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 redisPOOL = 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 responsedef setVariable(variable_name, variable_value):my_server = redis.Redis(connection_pool=POOL)my_server.set(variable_name, variable_value)

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

https://en.xdnf.cn/q/26321.html

Related Q&A

Writing a Python extension in Go (Golang)

I currently use Cython to link C and Python, and get speedup in slow bits of python code. However, Id like to use goroutines to implement a really slow (and very parallelizable) bit of code, but it mus…

Non-global middleware in Django

In Django there is a settings file that defines the middleware to be run on each request. This middleware setting is global. Is there a way to specify a set of middleware on a per-view basis? I wan…

I want to replace single quotes with double quotes in a list

So I am making a program that takes a text file, breaks it into words, then writes the list to a new text file.The issue I am having is I need the strings in the list to be with double quotes not singl…

How can I get stub files for `matplotlib`, `numpy`, `scipy`, `pandas`, etc.?

I know that the stub files for built-in Python library for type checking and static analysis come with mypy or PyCharm installation. How can I get stub files for matplotlib, numpy, scipy, pandas, etc.?…

Pipfile.lock out of date

Im trying to deploy a large django project to heroku. I installed Heroku CLI, logged in, created an app and ran:git push heroku masterI have a Pipfile and requirements.txt already set up. I added a run…

Can a simple difference in Python3 variable names alter the way code runs? [duplicate]

This question already has answers here:Python attributeError on __del__(2 answers)Closed 9 years ago.This code...class Person:num_of_people = 0def __init__(self, name):self.name = namePerson.num_of_peo…

Easy way to check that a variable is defined in python? [duplicate]

This question already has answers here:How do I check if a variable exists?(15 answers)Closed 10 years ago.Is there any way to check if a variable (class member or standalone) with specified name is d…

Adding install_requires to setup.py when making a python package

To make a python package, in setup.py, I have the following: setup(name=TowelStuff,version=0.1.0,author=J. Random Hacker,author_email=[email protected],packages=[towelstuff, towelstuff.test],scripts=[b…

pyqt: how to remove a widget?

I have a QGroupBox widget with children in it that I want to remove. How do I do that? I cant find any removeWidget, removeChild, removeItem, or anything similar in the docs. I can only see how to rem…

ValueError: cannot switch from manual field specification to automatic field numbering

The class:class Book(object):def __init__(self, title, author):self.title = titleself.author = authordef get_entry(self):return "{0} by {1} on {}".format(self.title, self.author, self.press)C…