Python connection redis002

Source: Internet
Author: User
Tags connection pooling redis server

Example 001,

Connect to Redis through Strictredis mode and call the GET command to read a string of value.

#!/usr/bin/python#!coding:utf-8ImportRedisif __name__=="__main__":    Try: R=redis. Strictredis (host='192.168.80.128', port=6379,db=0)#Strictredis Connect the Redis server in strict mode.         #1, do not realize slect        #2. Redis's del command instead of the Delete command (because del is also a keyword in python)        Print(R.get ('slogan'). Decode ('Utf-8'))#the Received object is of type byte    exceptException as err:Print(ERR)

By default, each Redis instance manages itself with a connection pool that is used to connect to the Redis-server side, which means that if there are multiple Redis objects in the program, there will be multiple connection pools, which is obviously not available.

So Redis also provides a way to share connection pooling.

Example 002,

Multiple Redis objects share a single connection pool.

#!/usr/bin/python#!coding:utf-8ImportRedisif __name__=="__main__":    Try: Conn_pool=redis. ConnectionPool (host='192.168.80.128', port=6379,db=0) R=redis. Redis (connection_pool=Conn_pool)#R=redis. Strictredis (host= ' 192.168.80.128 ', port=6379,db=0)        #Strictredis Connect the Redis server in strict mode.         #1, do not realize slect        #2. Redis's del command instead of the Delete command (because del is also a keyword in python)        Print(R.get ('slogan'). Decode ('Utf-8'))#the Received object is of type byte    exceptException as err:Print(ERR)

Python connection redis002

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.