Exemplo n.º 1
0
 def test_ConnectionPool(self):
     db = yield redis.ConnectionPool(redis_host,
                                     redis_port,
                                     poolsize=2,
                                     reconnect=False)
     self.assertEqual(isinstance(db, redis.ConnectionHandler), True)
     yield db.disconnect()
Exemplo n.º 2
0
 def test_ConnectionPool(self):
     db = yield redis.ConnectionPool(REDIS_HOST,
                                     REDIS_PORT,
                                     poolsize=2,
                                     reconnect=False)
     self.assertEqual(isinstance(db, redis.ConnectionHandler), True)
     yield db.disconnect()
Exemplo n.º 3
0
Arquivo: db.py Projeto: Lookyan/worker
 def startService(self):
     """
     startService(self)
     Creates redis connection pool
     """
     if self.rc is None:
         self.rc = yield redis.ConnectionPool(config.REDIS_HOST, config.REDIS_PORT, dbid=config.DBID)
     defer.returnValue(self)
Exemplo n.º 4
0
    def test_ConnectionPool(self):

        db = yield txredisapi.ConnectionPool(REDIS_HOST,
                                             REDIS_PORT,
                                             poolsize=2,
                                             reconnect=False)
        yield self._assert_simple_sets_on_pipeline(db=db)
        yield db.disconnect()
Exemplo n.º 5
0
 def testWatchAndPools_2(self):
     rapi = yield txredisapi.ConnectionPool(REDIS_HOST,
                                            REDIS_PORT,
                                            poolsize=2,
                                            reconnect=False)
     tx1 = yield rapi.watch("foobar")
     tx2 = yield rapi.watch("foobaz")
     self.assertTrue(id(tx1) != id(tx2))
     yield rapi.disconnect()
Exemplo n.º 6
0
 def connectionMade(self):
     try:
         self.redis = yield redis.ConnectionPool(self.cfg.get("redis", "host"),
                                                 self.cfg.getint("redis", "port"),
                                                 self.cfg.getint("redis", "db"),
                                                 True)
         self.pooling.start(5)
         xmppim.MessageProtocol.connectionMade(self)
     except:
         logger.exception()
 def receive_twisted(self, channels):
     """
     Twisted-native implementation of receive.
     """
     # List name get
     indexes = self._receive_list_names(channels)
     # Short circuit if no channels
     if indexes is None:
         defer.returnValue((None, None))
     # Get a message from one of our channels
     while True:
         got_expired_content = False
         # Try each index:channels pair at least once or until a result is returned
         for index, list_names in indexes.items():
             # Shuffle list_names to avoid the first ones starving others of workers
             random.shuffle(list_names)
             # Get a sync connection for conn details
             sync_connection = self.connection(index)
             twisted_connection = yield txredisapi.ConnectionPool(
                 host=sync_connection.connection_pool.
                 connection_kwargs['host'],
                 port=sync_connection.connection_pool.
                 connection_kwargs['port'],
                 dbid=sync_connection.connection_pool.
                 connection_kwargs['db'],
                 password=sync_connection.connection_pool.
                 connection_kwargs['password'],
             )
             try:
                 # Pop off any waiting message
                 result = yield twisted_connection.blpop(
                     list_names, timeout=self.blpop_timeout)
                 if result:
                     content = yield twisted_connection.get(result[1])
                     # If the content key expired, keep going.
                     if content is None:
                         got_expired_content = True
                         continue
                     # Return the channel it's from and the message
                     channel = result[0][len(self.prefix):]
                     message = self.deserialize(content)
                     # If there is a full channel name stored in the message, unpack it.
                     if "__asgi_channel__" in message:
                         channel = message['__asgi_channel__']
                         del message['__asgi_channel__']
                     defer.returnValue((channel, message))
             finally:
                 yield twisted_connection.disconnect()
         # If we only got expired content, try again
         if got_expired_content:
             continue
         else:
             defer.returnValue((None, None))
Exemplo n.º 8
0
    def __init__(self, namespace='queue'):
        self.namespace = namespace
        self.rc = None
        rc = redis.ConnectionPool(host=REDIS_HOST, port=REDIS_PORT)

        def con_established(result):
            self.rc = result
            log.info("Connection established to Redis")

        def conn_failed(f):
            log.error("Was not able to connect to Redis")
            print(f.getTraceback())

        rc.addCallback(con_established)
        rc.addErrback(conn_failed)
Exemplo n.º 9
0
def main():
    rc = yield redis.ConnectionPool()
    print (rc)

    # set
    yield rc.set("foo", "bar")

    # sleep, so you can kill redis
    print ("sleeping for 5s, kill redis now...")
    yield sleep(5)

    try:
      v = yield rc.get("foo")
      print ("foo:", v)

      yield rc.disconnect()
    except redis.ConnectionError as e:
      print (str(e))
Exemplo n.º 10
0
 def receive_twisted(self, channels):
     """
     Twisted-native implementation of receive.
     """
     # List name get
     indexes = self._receive_list_names(channels)
     # Short circuit if no channels
     if indexes is None:
         defer.returnValue((None, None))
     # Get a message from one of our channels
     while True:
         # Select a random connection to use
         index = random.choice(list(indexes.keys()))
         list_names = indexes[index]
         # Shuffle list_names to avoid the first ones starving others of workers
         random.shuffle(list_names)
         # Get a sync connection for conn details
         sync_connection = self.connection(index)
         twisted_connection = yield txredisapi.ConnectionPool(
             host=sync_connection.connection_pool.connection_kwargs['host'],
             port=sync_connection.connection_pool.connection_kwargs['port'],
             dbid=sync_connection.connection_pool.connection_kwargs['db'],
         )
         try:
             # Pop off any waiting message
             result = yield twisted_connection.blpop(
                 list_names, timeout=self.blpop_timeout)
             if result:
                 content = yield twisted_connection.get(result[1])
                 # If the content key expired, keep going.
                 if content is None:
                     continue
                 # Return the channel it's from and the message
                 channel = result[0][len(self.prefix):]
                 message = self.deserialize(content)
                 # If there is a full channel name stored in the message, unpack it.
                 if "__asgi_channel__" in message:
                     channel = message['__asgi_channel__']
                     del message['__asgi_channel__']
                 defer.returnValue((channel, message))
             else:
                 defer.returnValue((None, None))
         finally:
             yield twisted_connection.disconnect()
Exemplo n.º 11
0
    def testBlocking(self):
        db = yield redis.ConnectionPool(REDIS_HOST,
                                        REDIS_PORT,
                                        poolsize=2,
                                        reconnect=False)
        yield db.delete(self.QUEUE_KEY, self.TEST_KEY)

        # Block first connection.
        d = db.brpop(self.QUEUE_KEY, timeout=3)
        # Use second connection.
        yield db.set(self.TEST_KEY, 'somevalue')
        # Should use second connection again. Will block till end of
        # brpop otherwise.
        yield db.lpush('txredisapi:test_queue', self.QUEUE_VALUE)

        brpop_result = yield d
        self.assertNotEqual(brpop_result, None)

        yield db.delete(self.QUEUE_KEY, self.TEST_KEY)
        yield db.disconnect()
Exemplo n.º 12
0
    def test_ConnectionPool_managed_correctly(self):

        db = yield txredisapi.ConnectionPool(REDIS_HOST, REDIS_PORT, poolsize=1,
                                             reconnect=False)

        yield db.set('key1', 'value1')

        pipeline = yield db.pipeline()
        pipeline.get('key1')

        # We will yield after we finish the pipeline so we won't block here
        d = db.set('key2', 'value2')

        results = yield pipeline.execute_pipeline()

        # If the pipeline is managed correctly, there should only be one
        # response here
        self.assertEqual(len(results), 1)

        yield d
        yield db.disconnect()
Exemplo n.º 13
0
    def test_ConnectionPool(self):

        db = yield txredisapi.ConnectionPool(redis_host, redis_port, poolsize=2,
                                        reconnect=False)
        yield self._assert_simple_sets_on_pipeline(db=db)
        yield db.disconnect()
Exemplo n.º 14
0
 def Connect(self):
     self.redis = yield redis.ConnectionPool(self.server, self.port)
Exemplo n.º 15
0
 def testWatchAndPools_1(self):
     rapi = yield txredisapi.ConnectionPool(redis_host, redis_port, poolsize=2, reconnect=False)
     tx1 = yield rapi.watch("foobar")
     tx2 = yield tx1.watch("foobaz")
     self.assertTrue(id(tx1) == id(tx2))
     yield rapi.disconnect()
Exemplo n.º 16
0
 def __init__(self, engine):
     self.engine = engine
     self.server = None
     
     self.redisConnectionDefer = redis.ConnectionPool(host = '127.0.0.1', reconnect = True)
     self.redisConnectionDefer.addCallback(self.initPenguins)
Exemplo n.º 17
0
    def connect(self, poolsize=None):
        if not poolsize:
            poolsize = 100

        self.__redis = yield txredisapi.ConnectionPool(self.__host, poolsize=poolsize)