Redis installation, syntax and Python connection
Download the program http://down.51cto.com/data/2440789:
Click the client to test the connection. The following figure shows that the test is successful.
Normal connection redis
Pipes: you can execute multiple commands at a time
Import redispool = redis.ConnectionPool (host='127.0.0.1',port=6379) # connection pool # r = redis.Redis (host='127.0.0.1',port=6379) r = redis.Redis (connection_pool=pool) pipe = r.pipeline (transaction=True) r.set ('foo','bar') r.set (' zhangsan','heheheh') pipe.execute ()
Implement subscription and publishing
Server side (publisher side):
Import redispool = redis.ConnectionPool (host= "127.0.0.1", port='6379') rc = redis.Redis (connection_pool=pool) while True: mes = str (input ("Please enter what you want to publish:") rc.publish ('fm100.5', mes)
Client (subscriber):
Import redispool = redis.ConnectionPool (host= "127.0.0.1", port='6379') rc = redis.Redis (connection_pool=pool) ps = rc.pubsub () # Open ps.subscribe (['fm100.5']) # FM for item in ps.listen (): # listening if item [' type'] = 'message': data = item [' data'] print (data.decode ('utf-8'))
Official account of Wechat