Redis Publish Subscription (PUB/SUB) is a way of messaging: the publisher (pub) sends a message, and the Subscriber (sub) receives the message.
Redis clients can subscribe to any number of channels.
Introduction to related functions: 0.publish
Publish information to a specified channel 1.subscribe subscribe to one or more channels 2.psubscribe Subscribe to one or more channels that conform to a specific mode 3.unsubscribe unsubscribe one or more channels 4.punsubscribe unsubscribe to one or more specific mode channels
Example: Publishing End: publish.py
Redis
time
r = Redis. Redis () while
True:
time.sleep (1)
r.publish (' test1 ', ' hello ')
r.publish (' test2 ', ' the World ') )
r.publish (' foo ', ' msg from foo ')
r.publish (' foo1 ', ' msg from foo1 ')
r.publish (' Foo2 ', ' msg from Foo2 ') " C12/>r.publish (' Bar ', ' msg from bar ')
r.publish (' bar2 ', ' MSG-bar2 ')
r.publish (' Bar3 ', ' msg from BAR3 ') C15/>r.publish (' Foobar ', ' msg from Foobar ')
r.publish (' foobar2 ', ' msg from Foobar2 ')
r.publish (' Foobar3 ', ' msg from FOOBAR3 ')
Subscriber End: subscribe.py
Redis
r = Redis. Redis ()
p = r.pubsub ()
p.subscribe ([' test1 ', ' test2 ', ' test3 '])
p.psubscribe ([' foo* ', ' Bar? ', ' foobar+ ']
)p.listen ():
item[' type '] = = ' message ':
print (item[' channel ') ])
print (item[' data '))
P.unsubscribe (' test2 ')