示例#1
0
def regex_subscribe():
    '''Subscribes to messages on a topic that match a regex'''

    exchange = repubsub.Exchange('regex_demo', db='repubsub')

    category, chartype, character = random_topic()
    topic_regex = r'^{category}\.{chartype_character}$'.format(
        # This avoids regexes like 'fights\.(.+)\.Batman' where the
        # chartype can only be one thing.
        chartype_character = random.choice([
            chartype + '\.' + random.choice([character, '(.+)']),
            '(.+)',
        ]),
        category = random.choice([category, '(.+)']),
    )
    reql_filter = lambda topic: topic.match(topic_regex)
    queue = exchange.queue(reql_filter)

    sub_message = 'Subscribed to: %s' % topic_regex
    print_subscription(sub_message)

    for i, (topic, payload) in enumerate(queue.subscription()):
        if i % 20 == 19:
            # Reminder what we're subscribed to
            print_subscription(sub_message)

        print 'Received on', topic, ':', payload
示例#2
0
def regex_publish():
    '''Publishes messages to a simple string topic'''

    exchange = repubsub.Exchange('regex_demo', db='repubsub')

    while True:
        category, chartype, character = random_topic()
        topic_key = '{category}.{chartype}.{character}'.format(
            category=category, chartype=chartype, character=character)
        payload = random.choice(CATEGORIES[category])

        print 'Publishing on topic', topic_key, ':', payload

        exchange.topic(topic_key).publish(payload)
        time.sleep(0.5)
示例#3
0
def hierarchy_publish():
    '''Publishes messages on a hierarchical topic'''

    exchange = repubsub.Exchange('hierarchy_demo', db='repubsub')

    while True:
        topic_key, payload = random_hierarchy()

        print 'Publishing on hierarchical topic:'
        print_hierarchy(topic_key)
        print ' -', payload
        print

        exchange.topic(topic_key).publish(payload)
        time.sleep(0.5)
示例#4
0
def tags_publish():
    '''Publishes messages with an array of tags as a topic'''

    exchange = repubsub.Exchange('tags_demo', db='repubsub')
    
    while True:
        # Get two random topics, remove duplicates, and sort them
        # Sorting ensures that if two topics consist of the same
        # tags, the same document in the database will be updated
        # This should result in 270 possible tag values
        topic_tags = sorted(set(random_topic() + random_topic()))
        payload = random.choice(TEAMUPS + EVENTS + FIGHTS)

        print 'Publishing on tags #{}'.format(' #'.join(topic_tags))
        print '\t', payload

        exchange.topic(topic_tags).publish(payload)
        time.sleep(0.5)
示例#5
0
def tags_subscribe():
    '''Subscribes to messages that have specific tags in the topic'''

    exchange = repubsub.Exchange('tags_demo', db='repubsub')
    
    tags = random.sample(random_topic(), 2)
    reql_filter = lambda topic: topic.contains(*tags)
    queue = exchange.queue(reql_filter)

    sub_message = 'Subscribed to messages with tags: #%s' % ' #'.join(tags)
    print_subscription(sub_message)

    for i, (topic_tags, payload) in enumerate(queue.subscription()):
        if i % 10 == 9:
            # Reminder what we're subscribed to
            print_subscription(sub_message)

        print 'Received message with tags: #{}'.format(' #'.join(topic_tags))
        print '\t', payload
        print
示例#6
0
def hierarchy_subscribe():
    '''Subscribes to messages on a hierarchical topic'''

    exchange = repubsub.Exchange('hierarchy_demo', db='repubsub')

    category, chartype, character = random_topic()
    reql_filter = lambda topic: topic[category][chartype].contains(character)
    queue = exchange.queue(reql_filter)

    sub_message = 'Subscribed to topic: [%r][%r].contains(%r)' % (
        category, chartype, character)
    print_subscription(sub_message)

    for i, (topic, payload) in enumerate(queue.subscription()):
        if i % 5 == 4:
            # Reminder what we're subscribed to
            print_subscription(sub_message)

        print 'Received message with topic:'
        print_hierarchy(topic)
        print ' -', payload, '\n'