Redis in Python: Publish Subscriptions __python

Source: Internet
Author: User
Tags in python
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 ')


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.