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()
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()
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)
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()
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()
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))
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)
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))
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()
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()
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()
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()
def Connect(self): self.redis = yield redis.ConnectionPool(self.server, self.port)
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()
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)
def connect(self, poolsize=None): if not poolsize: poolsize = 100 self.__redis = yield txredisapi.ConnectionPool(self.__host, poolsize=poolsize)