コード例 #1
0
 def initialize(self):
     self.redis = tornadis.PubSubClient(autoconnect=False)
     yield self.redis.connect()
     result = yield self.redis.pubsub_psubscribe("item:*")
     print result
     loop = tornado.ioloop.IOLoop.current()
     loop.add_callback(self.watch_redis)
コード例 #2
0
def pubsub_coroutine():
    # Let's get a connected client
    # we don't use autoconnect=True because issue #22
    client = tornadis.PubSubClient(autoconnect=False)
    yield client.connect()

    # Let's "psubscribe" to a pattern
    yield client.pubsub_psubscribe("foo*")

    # Let's "subscribe" to a channel
    yield client.pubsub_subscribe("bar")

    # Looping over received messages
    while True:
        # Let's "block" until a message is available
        msg = yield client.pubsub_pop_message()
        print(msg)
        # >>> ['pmessage', 'foo*', 'foo', 'bar']
        # (for a "publish foo bar" command from another connection)

        if isinstance(msg, tornadis.TornadisException):
            # closed connection by the server
            break
        elif len(msg) >= 4 and msg[3] == "STOP":
            # it's a STOP message, let's unsubscribe and quit the loop
            yield client.pubsub_punsubscribe("foo*")
            yield client.pubsub_unsubscribe("bar")
            break

    # Let's disconnect
    client.disconnect()
コード例 #3
0
def pubsub_coroutine():
    # Let's get a connected client
    client = tornadis.PubSubClient()

    # Let's "psubscribe" to a pattern
    yield client.pubsub_psubscribe("foo*")

    # Let's "subscribe" to a channel
    yield client.pubsub_subscribe("bar")

    # Looping over received messages
    while True:
        # Let's "block" until a message is available
        msg = yield client.pubsub_pop_message()
        print(msg)
        # >>> ['pmessage', 'foo*', 'foo', 'bar']
        # (for a "publish foo bar" command from another connection)
        if len(msg) >= 4 and msg[3] == "STOP":
            # it's a STOP message, let's unsubscribe and quit the loop
            yield client.pubsub_punsubscribe("foo*")
            yield client.pubsub_unsubscribe("bar")
            break

    # Let's disconnect
    client.disconnect()
コード例 #4
0
 def get_client(self):
     client = tornadis.PubSubClient(
         host=self.redis_pub_sub_config['host'],
         port=self.redis_pub_sub_config['port'],
         password=self.redis_pub_sub_config['password'],
         autoconnect=self.autoconnect)
     return client
コード例 #5
0
    def add_subscriber(self, subscription_id, connection):
        """
        Add subscriber to a topic. It will register in redis if it is
        available (ie. redis parameter was passed to the constructor),
        otherwise, it will be a simple in memory operation only.
        """
        if self.redis_params is not None and self._subscriber_connection is None:
            self._subscriber_connection = tornadis.PubSubClient(
                autoconnect=False,
                ioloop=ioloop.IOLoop.current(),
                **self.redis_params)

            ret = utils.run_async(self._subscriber_connection.connect())
            if not ret:
                raise RedisUnavailableError(ret)

            try:
                ret = utils.run_async(
                    self._subscriber_connection.pubsub_subscribe(self.name))
            except TypeError:
                # workaround tornadis bug
                # (https://github.com/thefab/tornadis/pull/39)
                # This can be reached in Python 3.x
                raise RedisUnavailableError(str(self.redis_params))
            if not ret:
                # this will only be reached when the previously mentioned bug
                # is fixed
                # This can be reached in Python 2.7
                raise RedisUnavailableError(ret)

            self._register_redis_callback()
        self.subscribers[subscription_id] = connection
コード例 #6
0
 def listen(self):
     self.client = tornadis.PubSubClient(host="redis",
                                         port=6379,
                                         autoconnect=True,
                                         db=2)
     q = yield self.client.pubsub_subscribe("registered")
     while True:
         msg = yield self.client.pubsub_pop_message()
         username = b_utf(msg[2])
         for waiter in self.waiters:
             waiter[1].write_message(
                 json.dumps({
                     'act': 'iregistered',
                     'user': username,
                 }))
コード例 #7
0
        # result is already a python object (a string in this simple example)
        print "Result: %s" % result
    while True:
       result = yield client.pubsub_pop_message()
       if isinstance(result, tornadis.TornadisException):
           # For specific reasons, tornadis nearly never raises any exception
           # they are returned as result
           print "got exception: %s" % result
       else:
           # result is already a python object (a string in this simple example)
           print "Result: %s" % result


# Build a tornadis.Client object with some options as kwargs
# host: redis host to connect
# port: redis port to connect
# autoconnect=True: put the Client object in auto(re)connect mode

# Start a tornado IOLoop, execute the coroutine and end the program
ioloop = tornado.ioloop.IOLoop.instance()
#ioloop.run_sync(talk_to_redis)
client = tornadis.PubSubClient(host="localhost", port=6379, autoconnect=True)
ioloop.run_sync(talk_to_redis)
try:
  ioloop.start()
except KeyboardInterrupt:
  pass
finally:
  print "Closing server...\n"
  tornado.ioloop.IOLoop.instance().stop()