0

I am using the Redis client for Python called Redis-py. The GitHub library is located here https://github.com/andymccurdy/redis-py. I was wondering if there was any way to set the Redis database initialization values at runtime. For example, if I have a global variable called db and I want to change the value of db based on the configuration (dev, test, prod, etc), is there any way I can do this. Redispy only provides a constructor, but doesn't provide any way to initialize at runtime.

One way to do this is define the global variable db to be None and use a function to set change the db variable to the correct redis db. But, I am really trying to avoid using the global keyword/changing a global variable in a function. What are some other ways of doing this?

Yang K
  • 407
  • 4
  • 13
  • Please do not vandalise your post. This may result in a [question ban](http://stackoverflow.com/help/question-bans). By posting on the Stack Exchange network, you've granted a non-revocable right for SE to distribute that content (under the [CC BY-SA 3.0 license](https://creativecommons.org/licenses/by-sa/3.0/)). By SE policy, any vandalism will be reverted. If you would like to disassociate this post from your account, see [What is the proper route for a disassociation request?](https://meta.stackoverflow.com/q/323395) –  Mar 03 '19 at 02:55
  • Do not delete your question when you get your answer. See https://meta.stackoverflow.com/questions/378440/caveat-emptor-making-students-aware-they-cannot-delete-their-homework-questions. –  Mar 03 '19 at 02:56

1 Answers1

1

It's not clear if you're talking about dynamically changing the client configuration or the server configuration.

If you want to change the client configuration, you would just create a new connection by instantiating a new instance of the Redis object. You can see an example of that in the documentation:

>>> r = redis.Redis(host='localhost', port=6379, db=0)

If you want to "change" the db you'd merely create a new connection (i.e. Redis object) with the new db value.

If you're talking about dynamically changing the configuration of the Redis server, you can do that with the CONFIG SET command.

Kevin Christopher Henry
  • 46,175
  • 7
  • 116
  • 102
  • Related. Can I change the client side configuration dynamically? Say I have to connect to one redis, but I get a timeout, so I should be able to connect to another redis which has a different connection string. How do I go about this? Where/How do I change the code at the application level to get this done? – scientific_explorer Nov 14 '19 at 07:01
  • @scientific_explorer: I don't know, I doubt that capability is a documented part of the redis-py API which means that you would have to dig around in the internals. You should probably ask a new question about it. – Kevin Christopher Henry Nov 15 '19 at 06:36