def __init__(self, host='localhost', port=7711, password=None, socket_timeout=None, socket_connect_timeout=None, socket_keepalive=None, socket_keepalive_options=None, connection_pool=None, unix_socket_path=None, encoding='utf-8', encoding_errors='strict', decode_responses=False, retry_on_timeout=False, job_origin_ttl_secs=5, record_job_origin=False): """ job_origin_ttl_secs is the number of seconds to store counts of incoming jobs. The higher the throughput you're expecting, the lower this number should be. """ self.record_job_origin = record_job_origin kwargs = { 'password': password, 'socket_timeout': socket_timeout, 'encoding': encoding, 'encoding_errors': encoding_errors, 'decode_responses': decode_responses, 'retry_on_timeout': retry_on_timeout, 'db': 0, } # based on input, setup appropriate connection args if unix_socket_path is not None: kwargs.update({ 'path': unix_socket_path, 'connection_class': UnixDomainSocketConnection }) else: # TCP specific options kwargs.update({ 'host': host, 'port': port, 'socket_connect_timeout': socket_connect_timeout, 'socket_keepalive': socket_keepalive, 'socket_keepalive_options': socket_keepalive_options, }) if not connection_pool: connection_pool = ConnectionPool(**kwargs) self.response_callbacks = self.__class__.RESPONSE_CALLBACKS.copy() self.connection_pool = {'default': connection_pool} self.default_node = 'default' self._job_score = RollingCounter(ttl_secs=job_origin_ttl_secs) self.__connect_cluster(kwargs)
def test_rank(self): rc = RollingCounter() for _ in range(100): rc.add('foo') for _ in range(10): rc.add('bar') for _ in range(40): rc.add('baz') for _ in range(60): rc.add('quux') assert rc.max() == 'foo' assert rc.min() == 'bar' assert [x[0] for x in rc.ranked()] == ['bar', 'baz', 'quux', 'foo']
def test_expiration(self): rc = RollingCounter(ttl_secs=0.5) for _ in range(10): rc.add('foo') for _ in range(5): rc.add('bar') assert len(rc.keys()) == 2 assert rc.count('foo') == 10 time.sleep(1) assert len(rc.keys()) == 0 assert rc.max() is None assert rc.min() is None assert not rc.ranked() assert rc.count('foo') == 0