Python redis 模块,ConnectionPool() 实例源码

我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用redis.ConnectionPool()

项目:RTask    作者:HatBoy    | 项目源码 | 文件源码
def get_sysinfo(self):
        sysinfo = self.rpc.sysinfo(RPC_PWD)
        return sysinfo

# client = queues.get_redis_client(QUEUE_REDIS_TYPE, QUEUE_REDIS_HOST, QUEUE_REDIS_PORT, QUEUE_REDIS_DB, QUEUE_REDIS_PWD, QUEUE_REDIS_NODES)
# queues = queues.RedisQueues(conn=client)


# pool = redis.ConnectionPool(host=QUEUE_REDIS_HOST, port=QUEUE_REDIS_PORT, db=QUEUE_REDIS_DB, password=QUEUE_REDIS_PWD)
# client = redis.StrictRedis(connection_pool=pool)

# qi = QueueInfo()
# qi.taskfail_rpush({'taskid': '27050696', 'error': "module 'tasks.spider' has no attribute 'save'"})

# ni = NodeInfo()
# ni.task_list()
项目:RTask    作者:HatBoy    | 项目源码 | 文件源码
def __init__(self):
        self.pool = redis.ConnectionPool(host=RTASK_REDIS_HOST, port=RTASK_REDIS_POST, db=RTASK_REDIS_DB, password=RTASK_REDIS_PWD, encoding='utf-8', decode_responses=True)
        self.client = redis.StrictRedis(connection_pool=self.pool)
        self.macid = sysinfo.get_macid()
        self.ips = sysinfo.get_ips()
        self.hostname = sysinfo.get_hostname()
        self.platform = sysinfo.get_platform()
项目:lalascan    作者:blackye    | 项目源码 | 文件源码
def wait_parse_result(keys):
    pool = redis.ConnectionPool(host = REDIS_SERVER , port = 6379, db = 0, password = REDIS_PWD)
    r = redis.Redis(connection_pool = pool)
    spider_json_content = None
    while True:
        #TODO timeout is need
        try:
            _ = r.get(keys)
            if _ is not None:
                spider_json_content = _
                r.delete(keys)
                break
            else:
                time.sleep(2)
        except Exception:
            time.sleep(1)

    return spider_json_content
项目:kuzgun.io    作者:yigitgenc    | 项目源码 | 文件源码
def unix_time_millis(dt):
    """
    Convert datetime (dt) object to unix timestamp milliseconds.

    :param dt: datetime.datetime
    :return: float
    """
    naive = dt.replace(tzinfo=None)
    epoch = datetime.utcfromtimestamp(0)
    return (naive - epoch).total_seconds()


# Use ConnectionPool in order to set decode_response to True.
项目:scrapy_projects    作者:morefreeze    | 项目源码 | 文件源码
def get_pool(cls, redis_host, redis_port, redis_db):
        """build a redis connection
        :returns: a valid connection

        """
        try:
            pool = redis.ConnectionPool(host=redis_host, port=redis_port, db=redis_db)
            return redis.Redis(connection_pool=pool)
        except Exception as e:
            logging.error('connection redis error[%s]' % (e))
            raise
项目:scrapy_projects    作者:morefreeze    | 项目源码 | 文件源码
def __init__(self, name):
        """"""
        self.poped_name = self.POPED_FMT % (name)
        self.pushed_name = self.PUSHED_FMT % (name)
        pool = redis.ConnectionPool(host=self.HOST, port=self.PORT, db=self.DB)
        self.conn = redis.Redis(connection_=pool)
项目:OpsManage    作者:welliamcao    | 项目源码 | 文件源码
def getRedisConnection(db):
        '''?????????Redis???'''
        if db==APBase.REDSI_POOL:
            args = settings.REDSI_KWARGS_LPUSH
            if settings.REDSI_LPUSH_POOL == None:
                settings.REDSI_LPUSH_POOL = redis.ConnectionPool(host=args.get('host'), port=args.get('port'), db=args.get('db'))
            pools = settings.REDSI_LPUSH_POOL  
        connection = redis.Redis(connection_pool=pools)
        return connection
项目:IPProxy    作者:yutian2011    | 项目源码 | 文件源码
def __init__(
            self, capacity, tolerant,
            redis_conn={}):
        size, hash_count = self._get_cap(capacity, tolerant)
        self.redis_pool = redis.ConnectionPool(
            host=redis_conn.get('host', '127.0.0.1'),
            port=redis_conn.get('port', 6379),
            db=redis_conn.get('db', 0),
        )
        self.bf = BloomFilter(hash_count, size)
        self.bfkey = redis_conn.get('bfkey', 'bf')
项目:redisearch-py    作者:RedisLabs    | 项目源码 | 文件源码
def __init__(self, index_name, host='localhost', port=6379, conn=None):
        """
        Create a new Client for the given index_name, and optional host and port

        If conn is not None, we employ an already existing redis connection
        """

        self.index_name = index_name

        self.redis = conn if conn is not None else Redis(
            connection_pool=ConnectionPool(host=host, port=port))
项目:redisearch-py    作者:RedisLabs    | 项目源码 | 文件源码
def __init__(self, key, host='localhost', port=6379, conn = None):
        """
        Create a new AutoCompleter client for the given key, and optional host and port

        If conn is not None, we employ an already existing redis connection
        """

        self.key = key
        self.redis = conn if conn is not None else Redis(
            connection_pool = ConnectionPool(host=host, port=port))
项目:Ugly-Distributed-Crawler    作者:A1014280203    | 项目源码 | 文件源码
def make_redis_handler():
    pool = redis.ConnectionPool(host=r_server['ip'], port=r_server['port'],
                                password=r_server['passwd'])
    return redis.Redis(connection_pool=pool)
项目:Ugly-Distributed-Crawler    作者:A1014280203    | 项目源码 | 文件源码
def make_redis_handler():
    pool = redis.ConnectionPool(host=r_server['ip'], port=r_server['port'],
                                password=r_server['passwd'])
    return redis.Redis(connection_pool=pool)
项目:Ugly-Distributed-Crawler    作者:A1014280203    | 项目源码 | 文件源码
def connect_redis_server(self):
        pool = redis.ConnectionPool(host=r_server['ip'], port=r_server['port'], password=['passwd'])
        return redis.Redis(connection_pool=pool)
项目:Python_Study    作者:thsheep    | 项目源码 | 文件源码
def redis_pool():
    pool = redis.ConnectionPool(host='localhost', port=6379)
    r = redis.Redis(connection_pool=pool)
    r.set('name', 'zhangsan')   # ??
    print (r.get('name'))       # ??
项目:Python_Study    作者:thsheep    | 项目源码 | 文件源码
def redis_pipe():
    pool = redis.ConnectionPool(host='localhost', port=6379)
    r = redis.Redis(connection_pool=pool)
    pipe = r.pipeline(transaction=True)
    r.set('name', 'zhangsan')
    r.set('name', 'lisi')
    pipe.execute()
项目:github_search    作者:facert    | 项目源码 | 文件源码
def get_redis():
    redis_conf = {
        'host': '127.0.0.1',
        'port': 6379,
        'db': 0
    }
    pool = redis.ConnectionPool(host=redis_conf['host'], port=redis_conf['port'], db=redis_conf['db'])
    return redis.StrictRedis(connection_pool=pool)
项目:kuberdock-platform    作者:cloudlinux    | 项目源码 | 文件源码
def lookup_pool(cls, *args, **kwargs):
        key = cls.key(*args, **kwargs)
        if key not in cls.pool:
            cls.pool[key] = redis.ConnectionPool(*args, **kwargs)
        return cls.pool[key]
项目:kuberdock-platform    作者:cloudlinux    | 项目源码 | 文件源码
def __init__(self, name, ttl=None):
        """Init Lock object.
        :param name: name of the lock
        :param ttl: number of seconds after acquiring when the lock must
        be automatically released.
        """
        self._redis_con = ConnectionPool().get_connection()
        self._lock = None
        self.name = self.lock_prefix + name
        self.ttl = ttl
项目:kuberdock-platform    作者:cloudlinux    | 项目源码 | 文件源码
def is_acquired(cls, name):
        """Checks if the lock was already acquired and not yet released."""
        redis_con = ConnectionPool().get_connection()
        name = cls.lock_prefix + name
        lock = redis_con.lock(name, 1)
        res = False
        try:
            res = not lock.acquire(blocking=False)
        finally:
            try:
                lock.release()
            except redis.lock.LockError:
                # exception is raised in case of already released lock
                pass
        return res
项目:kuberdock-platform    作者:cloudlinux    | 项目源码 | 文件源码
def clean_locks(cls, pattern=None):
        """Removes all locks. Optionally may be specified prefix for lock's
        names.

        """
        redis_con = ConnectionPool().get_connection()
        if pattern:
            pattern = cls.lock_prefix + pattern + '*'
        else:
            pattern = cls.lock_prefix + '*'
        keys = list(redis_con.scan_iter(pattern))
        if keys:
            redis_con.delete(*keys)
项目:minerva    作者:linzhi    | 项目源码 | 文件源码
def __init__(self, host, port):

        self.conn = None

        connKey = "%s:%s" % (host, port)
        if connKey not in RedisHandler.connPool:
            pool = redis.ConnectionPool(host=host, port=int(port))
            r = redis.StrictRedis(connection_pool=pool)
            RedisHandler.connPool[connKey] = r
        self.conn = RedisHandler.connPool[connKey]
        self.retry_times = RedisHandler.DEFAULT_RETRY_TIMES
项目:DistributeCrawler    作者:SmallHedgehog    | 项目源码 | 文件源码
def getRedis(**kwargs):
    """Get the instance of redis."""
    pool = redis.ConnectionPool(**kwargs)
    return redis.Redis(connection_pool=pool)
项目:lampost_lib    作者:genzgd    | 项目源码 | 文件源码
def __init__(self, db_host, db_port, db_num, db_pw):
        self.pool = ConnectionPool(max_connections=2, db=db_num, host=db_host, port=db_port, password=db_pw,
                                   decode_responses=True)
        self.redis = StrictRedis(connection_pool=self.pool)
        self.redis.ping()
        self._object_map = WeakValueDictionary()
项目:ziyan    作者:maboss-YCMan    | 项目源码 | 文件源码
def __init__(self, conf):
        """
        :param conf: dict, ?? Redis ? host, port, db
        """
        pool = redis.ConnectionPool(
            host=conf.get('host', 'localhost'),
            port=conf.get('port', 6379),
            db=conf.get('db', 0))
        self.__db = redis.StrictRedis(connection_pool=pool, socket_timeout=1)

        # ??redis???
        self.test_connect()
项目:thought    作者:rsj217    | 项目源码 | 文件源码
def create_redis():
    """ ?? redis ?? """
    connection_pool = redis.ConnectionPool(
        host=config.REDISDB['host'],
        port=config.REDISDB['port'],
        db=config.REDISDB['db'],
        password=config.REDISDB['password']
    )

    return redis.Redis(connection_pool=connection_pool)
项目:Malicious_Domain_Whois    作者:h-j-13    | 项目源码 | 文件源码
def _get_proxy():
    pool = redis.ConnectionPool(host='172.26.253.91', port=6379)
    r = redis.StrictRedis(connection_pool=pool, charset='utf-8')
    proxys_str = r.get("0")
    print proxys_str
    return eval(proxys_str)
项目:Malicious_Domain_Whois    作者:h-j-13    | 项目源码 | 文件源码
def _get_proxy():
    pool = redis.ConnectionPool(host='172.26.253.91', port=6379)
    r = redis.StrictRedis(connection_pool=pool, charset='utf-8')
    proxys_str = r.get("0")
    print proxys_str
    return eval(proxys_str)
项目:Malicious_Domain_Whois    作者:h-j-13    | 项目源码 | 文件源码
def _get_proxy():
    pool = redis.ConnectionPool(host='172.26.253.91', port=6379)
    r = redis.StrictRedis(connection_pool=pool, charset='utf-8')
    proxys_str = r.get("0")
    print proxys_str
    return eval(proxys_str)
项目:pycon2016    作者:nhudinhtuan    | 项目源码 | 文件源码
def __init__(self, config):
        BaseCache.__init__(self, config)
        self.key_prefix = config.get('key_prefix', '')
        try:
            import redis
        except ImportError:
            raise RuntimeError('no redis module found')
        kwargs = dict((k, v) for k, v in config.items() if k not in _redis_kwargs_exclusions)
        if 'socket_timeout' not in kwargs:
            kwargs['socket_timeout'] = _DEFAULT_SOCKET_TIMEOUT
        if 'socket_connect_timeout' not in kwargs:
            kwargs['socket_connect_timeout'] = _DEFAULT_SOCKET_TIMEOUT
        if 'socket_keepalive' not in kwargs:
            kwargs['socket_keepalive'] = 1
        if 'socket_keepalive_options' not in kwargs:
            kwargs['socket_keepalive_options'] = _TCP_KEEP_ALIVE_OPTIONS
        if kwargs.pop('blocking_pool', False):
            if 'blocking_pool_timeout' in kwargs:
                kwargs['timeout'] = kwargs.pop('blocking_pool_timeout')
            else:
                kwargs['timeout'] = _DEFAULT_REDIS_BLOCKING_POOL_TIMEOUT
            connection_pool = redis.BlockingConnectionPool(**kwargs)
        else:
            connection_pool = redis.ConnectionPool(**kwargs)

        self._client = redis.Redis(connection_pool=connection_pool)
项目:webTimer    作者:EchoUtopia    | 项目源码 | 文件源码
def __init__(self):
        self._pool = redis.ConnectionPool(**conf.REDIS_CACHE)
        self.redis = redis.Redis(connection_pool=self._pool)
        self._persist_pool = redis.ConnectionPool(**conf.REDIS_PERSIST)
        self.persist_redis = redis.Redis(connection_pool=self._persist_pool)
项目:crawler    作者:dragonflylxp    | 项目源码 | 文件源码
def get_redis(dbid, standalone=False):
    conf = configs['redis'][dbid].copy()
    if standalone:
        conf.pop('max_connections', None)
        return redis.Redis(**conf)
    pool = redis_pools.get(dbid)
    if not pool:
        conf.setdefault('max_connections', 8)
        pool = redis.ConnectionPool(**conf)
        redis_pools[dbid] = pool
    return redis.Redis(connection_pool=pool)


########################################
项目:automatic-repo    作者:WZQ1397    | 项目源码 | 文件源码
def __init__ (self,redis_hostname,redis_port):
        # pool = redis.ConnectionPool(host=, port=)
        self.__conn = redis.Redis(host=redis_hostname, port=redis_port)
项目:fluiddb    作者:fluidinfo    | 项目源码 | 文件源码
def getCacheConnectionPool():
    """Get a Redis connection pool.

    @return: A L{redis.ConnectionPool} object or None if one hasn't been
        registered.
    """
    return _cacheConnectionPool
项目:fluiddb    作者:fluidinfo    | 项目源码 | 文件源码
def setCacheConnectionPool(connectionPool):
    """Set the Redis connection pool.

    @param: A L{redis.ConnectionPool} object.
    """
    global _cacheConnectionPool
    _cacheConnectionPool = connectionPool
项目:fluiddb    作者:fluidinfo    | 项目源码 | 文件源码
def setupCache(config):
    """Setup the Redis Cache

    A new L{redis.ConnectionPool} is created using the values defined in the
    configuration, and then registered with L{setCacheConnectionPool}

    @param config: a configuration instance.
    @return a L{redis.ConnectionPool}.
    """
    host = config.get('cache', 'host')
    port = config.getint('cache', 'port')
    db = config.getint('cache', 'db')
    connectionPool = ConnectionPool(host=host, port=port, db=db)
    setCacheConnectionPool(connectionPool)
    return connectionPool
项目:fluiddb    作者:fluidinfo    | 项目源码 | 文件源码
def make(self, dependency_resources):
        host = '127.0.0.1'
        port = 6379
        db = 1  # Use DB number 1 instead of 0 for testing purposes.
        db = int(os.environ.get('FLUIDDB_CACHE', db))
        connectionPool = ConnectionPool(host=host, port=port, db=db)
        setCacheConnectionPool(connectionPool)
        client = Redis(connection_pool=connectionPool)
        client.flushdb()  # Delete everything from the cache.
        return client
项目:fluiddb    作者:fluidinfo    | 项目源码 | 文件源码
def clean(self, client):
        host = '127.0.0.1'
        # Use a broken port to simulate Redis being unavailable.
        port = 9999
        db = 1  # Use DB number 1 instead of 0 for testing purposes.
        connectionPool = ConnectionPool(host=host, port=port, db=db)
        setCacheConnectionPool(connectionPool)
项目:fluiddb    作者:fluidinfo    | 项目源码 | 文件源码
def testGetValuesWithError(self):
        """
        Redis errors are ignored by L{BaseCache.getValues} and a line is
        written in the logs.
        """
        cache = BaseCache()
        cache._client.connection_pool = ConnectionPool(port=0)
        result = cache.getValues([u'identifier1'])
        self.assertIdentical(None, result)
        self.assertEqual('Redis error: Error 111 connecting localhost:0. '
                         'Connection refused.\n', self.log.getvalue())
项目:fluiddb    作者:fluidinfo    | 项目源码 | 文件源码
def testSetValuesWithError(self):
        """
        Redis errors are ignored by L{BaseCache.setValues} and a line is
        written in the logs.
        """
        cache = BaseCache()
        cache._client.connection_pool = ConnectionPool(port=0)
        cache.setValues({'identifier': 'test'})
        self.assertEqual('Redis error: Error 111 connecting localhost:0. '
                         'Connection refused.\n', self.log.getvalue())
项目:fluiddb    作者:fluidinfo    | 项目源码 | 文件源码
def testDeleteValuesWithError(self):
        """
        Redis errors are ignored by L{BaseCache.deleteValues} and a line is
        written in the logs.
        """
        cache = BaseCache()
        cache._client.connection_pool = ConnectionPool(port=0)
        cache.deleteValues(['identifier'])
        self.assertEqual('Redis error: Error 111 connecting localhost:0. '
                         'Connection refused.\n', self.log.getvalue())
项目:wxBot    作者:kzx1025    | 项目源码 | 文件源码
def create_pool():
        RedisCache.pool = redis.ConnectionPool(
                host = RedisDBConfig.HOST,
                port = RedisDBConfig.PORT,
                db   = RedisDBConfig.DBID)
项目:RTask    作者:HatBoy    | 项目源码 | 文件源码
def __init__(self):
        self.pool = redis.ConnectionPool(host=RTASK_REDIS_HOST, port=RTASK_REDIS_POST, db=RTASK_REDIS_DB, password=RTASK_REDIS_PWD, encoding='utf-8', decode_responses=True)
        self.client = redis.StrictRedis(connection_pool=self.pool)
项目:RTask    作者:HatBoy    | 项目源码 | 文件源码
def get_redis_client(redis_type='single', host='127.0.0.1', port=6379, db=0, pwd=None, nodes=None, timeout=3):
    if redis_type == 'single':
        pool = redis.ConnectionPool(host=host, port=port, db=db, password=pwd, socket_timeout=timeout, socket_connect_timeout=timeout, encoding='utf-8', decode_responses=True)
        client = redis.StrictRedis(connection_pool=pool)
    else:
        client = rediscluster.StrictRedisCluster(startup_nodes=nodes, decode_responses=True, socket_timeout=timeout, socket_connect_timeout=timeout)
    return client
项目:BloomFilterRedis    作者:kongtianyi    | 项目源码 | 文件源码
def __init__(self, key, host='172.0.0.1', port=6379, hash_list=hash_list):
        # redis-bitmap?key
        self.key = key
        # redis????
        self.pool = redis.ConnectionPool(host=host, port=port)
        self.handle = redis.StrictRedis(connection_pool=self.pool, charset='utf-8')
        # ??????
        self.hash_list = hash_list
项目:BloomFilterRedis    作者:kongtianyi    | 项目源码 | 文件源码
def __init__(self, key, host='172.0.0.1', port=6379, hash_list=hash_list):
        # redis-bitmap?key
        self.key = key
        # redis????
        self.pool = redis.ConnectionPool(host=host, port=port)
        self.handle = redis.StrictRedis(connection_pool=self.pool, charset='utf-8')
        # ??????
        self.hash_list = hash_list
项目:BloomFilterRedis    作者:kongtianyi    | 项目源码 | 文件源码
def __init__(self, key, host='172.0.0.1', port=6379, hash_list=hash_list):
        # redis-bitmap?key
        self.key = key
        # redis????
        self.pool = redis.ConnectionPool(host=host, port=port)
        self.handle = redis.StrictRedis(connection_pool=self.pool, charset='utf-8')
        # ??????
        self.hash_list = hash_list
项目:Cuppa    作者:flipkart-incubator    | 项目源码 | 文件源码
def __init__(self, host, port, database, password):
        self.host = host
        self.port = port
        self.database = database
        self.password = password
        self.pool = redis.ConnectionPool(host=self.host, port=self.port, db=self.database)
        self.connection = redis.StrictRedis(password=self.password, connection_pool=self.pool)
        self.logger = self.logger = logging.getLogger(__name__)
        self.logger.info("[REDIS Inited]")
项目:sea    作者:shanbay    | 项目源码 | 文件源码
def init_app(self, app):
        opts = app.config.get_namespace('REDIS_')
        self._pool = redis.ConnectionPool(**opts)
        self._client = redis.StrictRedis(connection_pool=self._pool)
项目:python_data_tools    作者:king3366ster    | 项目源码 | 文件源码
def redisConnect(self, redis_db, use_pool = True):
        if 'host' in redis_db:
            _host = redis_db['host']
        else:
            _host = '127.0.0.1'
        self._host_redis = _host

        if 'db' in redis_db:
            _db = redis_db['db']
        else:
            _db = 0
        self._db_redis = _db

        if 'port' in redis_db:
            _port = redis_db['port']
        else:
            _port = 6379
        self._port_redis = _port

        if 'auth' in redis_db:
            _auth = redis_db['auth']
        else:
            _auth = None
        self._auth_redis = _auth

        try:
            if use_pool:
                redis_pool = redis.ConnectionPool(host = _host, port = _port, db = _db, password = _auth)
                self.conn_redis = redis.Redis(connection_pool = redis_pool)
            else:
                self.conn_redis = redis.Redis(host = _host, port = _port, db = _db, password = _auth)
            print 'Redis connected %s %s' % (_host, _port)
        except redis.RedisError as e:
            print 'Redis Error: %s' % e
项目:falcon-api-redis    作者:kentsay    | 项目源码 | 文件源码
def connection(self):
        pool = redis.ConnectionPool(host=config.REDIS_URL, port=config.REDIS_PORT, db=config.REDIS_DB)
        r = redis.Redis(connection_pool=pool)
        return r