In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "the method of installing redis-py and creating connection pool in Python". In the daily operation, I believe that many people have doubts about the method of installing redis-py and creating connection pool in Python. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how to install redis-py in Python and how to create connection pool". Next, please follow the editor to study!
Install redis-pypip3 install redis
# # connecting to redis
Redis-py provides two classes, Redis and StrictRedis, to implement Redis commands. StrictRedis is used to implement most official commands and uses official syntax and commands. Redis is a subclass of StrictRedis.
Import redisr = redis.StrictRedis (host='localhost', port=6379, db=0) r.set ('foo',' bar') c = r.ge connection pool
Redis-py uses connection pool to manage all connections to a redis server, avoiding the overhead of establishing and releasing connections each time. By default, each Redis instance maintains its own connection pool. You can directly establish a connection pool, and then as a parameter Redis, so that multiple Redis instances can share a connection pool.
Import redispool = redis.ConnectionPool (host='localhost', port=6379, decode_responses=True) clientOne = redis.Redis (connection_pool=pool) clientTwo = redis.Redis (connection_pool=pool) clientOne.set ('name1',' zhangsan') print (clientOne.get ('name1')) clientTwo.set (' name2', 'lisi') print (clientTwo.get (' name2')) print (clientOne.client_list ()) print (clientTwo.client_list ()) # it can be seen that the two connected id are consistent It means it's a client connection.
# # Pipeline
By default, redis-py creates (connection pooling requests for connections) and disconnects (returns connection pooling) for each request. If you want to specify multiple commands in one request, you can use pipline to specify multiple commands for one request, and pipline is atomic by default.
Import redispool = redis.ConnectionPool (host='localhost', port=6379) r = redis.Redis (connection_pool=pool) pipe = r.pipeline () r.set ('name',' zhangsan') r.set ('name',' lisi') r.set ('name',' wangwu') pipe.execute () so far, the study on "how to install redis-py in Python and how to create a connection pool" is over. I hope you can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.