Python Connect to Redis

Source: Internet
Author: User
Tags connection pooling redis server

Redis: is a high-performance Key-value database.

Simply put: When you use a relational database, files, or other media to store things, performance is slow to respond slowly, this time you need to take something to cache. There are several kinds of similar products, at present more popular, in our business is this. At present, this thing in the cluster state, for our millions concurrency to provide good support.

Today, let's talk a little bit about how to operate Redis with Python.

Niu Mei, put this into your graduation design, but also the icing on the cake Oh.

Redis Installation, reference article

1.python Connecting Redis

Normal connection

Import Redis

#后面是地址 Port
R = Redis. Redis (host= ' 127.0.0.1 ', port=6379)

2. Connection pooling

Redis-py uses connection pool to manage all connections to a Redis server, avoiding the overhead of each build and release of the connection

Import Redis

Pool = Redis. ConnectionPool (host= ' 192.168.0.110 ', port=6379)
R = Redis. Redis (Connection_pool=pool)

3. Piping

Redis-py, by default, connects and disconnects the connection pool each time. If you want to execute more than one command at a time, you need to use a pipeline for transactional operations.

Import Redis

Pool = Redis. ConnectionPool (host= ' 192.168.0.110 ', port=6379)
R = Redis. Redis (Connection_pool=pool)
Pipe = R.pipeline (transaction=true)

R.set (' zcx ', ' 5555 ')
R.set (' zcx ', ' 6666 ')

Pipe.execute ()

Description: Although this feature is provided. But the actual situation is not recommended to use. Because this is slow to execute. On the other hand, there is no need

4. Publish a subscription

While we were learning the operating system, there was a producer and a consumer in the book. Here, I try to use Redis to simulate

Defining common classes, wrapping methods, and initializing connections

Import Redis
Class Redis_util (object):
def __init__ (self):
Self.__conn = Redis. Redis (host= ' 127.0.0.1 ', port=6379)
Self.channel = ' Mybaby ' # defines the channel name

def publish (self, msg): # Defines methods for publishing
Self.__conn.publish (Self.channel, msg)
Return True

def subscribe (self): # Methods for defining subscriptions
Pub = Self.__conn.pubsub ()
Pub.subscribe (Self.channel)
Pub.parse_response ()
Return pub

Publisher Publisher:

From polls.redis_utils import Redis_util

obj = Redis_util ()
Obj.publish (' Test ')

Subscriber Subscribers:

From polls.redis_utils import Redis_util

obj = Redis_util ()
Redis_sub = Obj.subscribe ()

While True:
msg = Redis_sub.parse_response ()
Print (msg)

As for publishing subscriptions. Just to say that there is such a function. However, it is not recommended. Later, I will discuss this issue with you in more depth.

All right, we're going to be here tonight, to accompany my little baby.

Python Connect to Redis

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.