The interaction between Python and Redis

Source: Internet
Author: User
Tags connection pooling install redis

The interaction between Python and Redis one. Redis Module

Installation module:

PIP3 Install Redis

Connection mode:

R = Redis. Redis (host= ' localhost ', port=6379)

Connection pooling: To conserve resources, reduce the consumption of multiple connections.

Pool=redis. ConnectionPool (host= ' localhost ', port=6379,decode_responses=true)

Two. Redis Operations

General Operation:

import redisr = redis.Redis(host=‘localhost‘,port=6379)r.set(‘foo‘,‘bar‘)print(r.get(‘foo‘))

Connection pool:

import redispool = redis.ConnectionPool(host=‘localhost‘,port=6379,decode_responses=True)# 默认设置的值和取得的值都是bytes类型,如果想改为str类型,可以添加decode_responses=Truer1 = redis.Redis(connection_pool=pool)r2 = redis.Redis(connection_pool=pool)r1.set(‘name‘,‘jack‘)print(r1.get(‘name‘))r2.set(‘age‘,18)print(r2.get(‘age‘))print(r1.client_list())print(r2.client_list())

Pipeline:

import redis,timer = redis.Redis(host=‘localhost‘,port=6379,decode_responses=True)pipe = r.pipeline(transaction=True)pipe.set(‘name1‘,‘Alin‘)pipe.set(‘name2‘,‘Lnda‘)pipe.set(‘name3‘,‘Tony‘)time.sleep(5) pipe.execute()print(r.mget(‘name1‘,‘name2‘,‘name3‘))

Transactions: Python can use pipelines instead of transactions

import redis,timeimport redis.exceptionsr = redis.Redis(host=‘localhost‘,port=6379,decode_responses=True)pipe = r.pipeline()print(r.get(‘name1‘))try:    pipe.multi()    pipe.set(‘age1‘,22)    pipe.set(‘age2‘,23)    pipe.set(‘age3‘,24)    time.sleep(5)    pipe.execute()    print(r.mget(‘age1‘,‘age2‘,‘age3‘))except redis.exceptions.WatchError as e:    print(‘Error‘)

Subscribe and publish:
Publisher:

import redisr = redis.Redis(host=‘localhost‘,port=6379,decode_responses=True)while True:    msg = input(‘echo>>:‘)    if len(msg) == 0:        continue    elif msg == ‘quit‘:        r.publish(‘cctv1‘,msg)        break    else:        r.publish(‘cctv1‘,msg)

Subscribers:

import redisr = redis.Redis(host=‘localhost‘,port=6379,decode_responses=True)chan = r.pubsub() #返回一个发布订阅对象msg_reciver = chan.subscribe(‘cctv1‘) #订阅msg = chan.parse_response()  # 返回一个确认print(msg)print(‘订阅成功,开始接收...‘)while True:    msg = chan.parse_response()  #接收消息    if msg[2] == ‘quit‘:  #格式:类型,频道,消息        break    else:        print(‘>>:‘, msg[2])

The interaction between Python and 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.