Python redis.StrictRedis() Examples

The following are 30 code examples for showing how to use redis.StrictRedis(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module redis , or try the search function .

Example 1
Project: trader   Author: BigBrotherTrade   File: __init__.py    License: Apache License 2.0 6 votes vote down vote up
def __init__(self, io_loop: asyncio.AbstractEventLoop = None):
        super().__init__()
        self.io_loop = io_loop or asyncio.get_event_loop()
        self.sub_client = self.io_loop.run_until_complete(
                aioredis.create_redis((config.get('REDIS', 'host', fallback='localhost'),
                                       config.getint('REDIS', 'port', fallback=6379)),
                                      db=config.getint('REDIS', 'db', fallback=1)))
        self.redis_client = redis.StrictRedis(
            host=config.get('REDIS', 'host', fallback='localhost'),
            db=config.getint('REDIS', 'db', fallback=1), decode_responses=True)
        self.initialized = False
        self.sub_tasks = list()
        self.sub_channels = list()
        self.channel_router = dict()
        self.crontab_router = defaultdict(dict)
        self.datetime = None
        self.time = None
        self.loop_time = None 
Example 2
Project: pyspider   Author: binux   File: redis_queue.py    License: Apache License 2.0 6 votes vote down vote up
def __init__(self, name, host='localhost', port=6379, db=0,
                 maxsize=0, lazy_limit=True, password=None, cluster_nodes=None):
        """
        Constructor for RedisQueue

        maxsize:    an integer that sets the upperbound limit on the number of
                    items that can be placed in the queue.
        lazy_limit: redis queue is shared via instance, a lazy size limit is used
                    for better performance.
        """
        self.name = name
        if(cluster_nodes is not None):
            from rediscluster import StrictRedisCluster
            self.redis = StrictRedisCluster(startup_nodes=cluster_nodes)
        else:
            self.redis = redis.StrictRedis(host=host, port=port, db=db, password=password)
        self.maxsize = maxsize
        self.lazy_limit = lazy_limit
        self.last_qsize = 0 
Example 3
Project: seqr   Author: macarthur-lab   File: variant_utils.py    License: GNU Affero General Public License v3.0 6 votes vote down vote up
def reset_cached_search_results(project):
    try:
        redis_client = redis.StrictRedis(host=REDIS_SERVICE_HOSTNAME, socket_connect_timeout=3)
        keys_to_delete = []
        if project:
            result_guids = [res.guid for res in VariantSearchResults.objects.filter(families__project=project)]
            for guid in result_guids:
                keys_to_delete += redis_client.keys(pattern='search_results__{}*'.format(guid))
        else:
            keys_to_delete = redis_client.keys(pattern='search_results__*')
        if keys_to_delete:
            redis_client.delete(*keys_to_delete)
            logger.info('Reset {} cached results'.format(len(keys_to_delete)))
        else:
            logger.info('No cached results to reset')
    except Exception as e:
        logger.error("Unable to reset cached search results: {}".format(e)) 
Example 4
Project: distributed_rl   Author: neka-nat   File: learner.py    License: MIT License 6 votes vote down vote up
def __init__(self, policy_net, target_net, optimizer,
                 vis, replay_size=30000, hostname='localhost',
                 beta_decay=1000000,
                 use_memory_compress=False):
        self._vis = vis
        self._policy_net = policy_net
        self._target_net = target_net
        self._target_net.load_state_dict(self._policy_net.state_dict())
        self._target_net.eval()
        self._beta_decay = beta_decay
        self._connect = redis.StrictRedis(host=hostname)
        self._connect.delete('params')
        self._optimizer = optimizer
        self._win = self._vis.line(X=np.array([0]), Y=np.array([0]),
                                   opts=dict(title='Memory size'))
        self._win2 = self._vis.line(X=np.array([0]), Y=np.array([0]),
                                    opts=dict(title='Q loss'))
        self._memory = replay.Replay(replay_size, self._connect,
                                     use_compress=use_memory_compress)
        self._memory.start() 
Example 5
Project: distributed_rl   Author: neka-nat   File: actor.py    License: MIT License 6 votes vote down vote up
def __init__(self, actor_no, env, policy_net, vis, hostname='localhost',
                 batch_size=50, nstep_return=3, gamma=0.999,
                 clip=lambda x: min(max(-1.0, x), 1.0),
                 target_update=200, num_total_actors=4,
                 device=torch.device("cuda" if torch.cuda.is_available() else "cpu")):
        self._env = env
        self._actor_no = actor_no
        self._name = "actor_" + str(actor_no)
        self._vis = vis
        self._batch_size = batch_size
        self._nstep_return = nstep_return
        self._gamma = gamma
        self._clip = clip
        self._target_update = target_update
        self._num_total_actors = num_total_actors
        self._policy_net = policy_net
        self._policy_net.eval()
        self._device = device
        self._win1 = self._vis.image(utils.preprocess(self._env.env._get_image()))
        self._win2 = self._vis.line(X=np.array([0]), Y=np.array([0.0]),
                                    opts=dict(title='Score %s' % self._name))
        self._local_memory = replay_memory.ReplayMemory(1000)
        self._connect = redis.StrictRedis(host=hostname) 
Example 6
Project: scrapy-cluster   Author: istresearch   File: rest_service.py    License: MIT License 6 votes vote down vote up
def _setup_redis(self):
        """Returns a Redis Client"""
        if not self.closed:
            try:
                self.logger.debug("Creating redis connection to host " +
                                  str(self.settings['REDIS_HOST']))
                self.redis_conn = redis.StrictRedis(host=self.settings['REDIS_HOST'],
                                              port=self.settings['REDIS_PORT'],
                                              db=self.settings['REDIS_DB'])
                self.redis_conn.info()
                self.redis_connected = True
                self.logger.info("Successfully connected to redis")
            except KeyError as e:
                self.logger.error('Missing setting named ' + str(e),
                                   {'ex': traceback.format_exc()})
            except:
                self.logger.error("Couldn't initialize redis client.",
                                   {'ex': traceback.format_exc()})
                raise 
Example 7
Project: sentry-python   Author: getsentry   File: redis.py    License: BSD 2-Clause "Simplified" License 6 votes vote down vote up
def setup_once():
        # type: () -> None
        import redis

        patch_redis_client(redis.StrictRedis)

        try:
            import rb.clients  # type: ignore
        except ImportError:
            pass
        else:
            patch_redis_client(rb.clients.FanoutClient)
            patch_redis_client(rb.clients.MappingClient)
            patch_redis_client(rb.clients.RoutingClient)

        try:
            _patch_rediscluster()
        except Exception:
            logger.exception("Error occured while patching `rediscluster` library") 
Example 8
Project: zou   Author: cgwire   File: publisher_store.py    License: GNU Affero General Public License v3.0 6 votes vote down vote up
def init():
    """
    Initialize key value store that will be used for the event publishing.
    That way the main API takes advantage of Redis pub/sub capabilities to push
    events to the event stream API.
    """
    global socketio

    try:
        publisher_store = redis.StrictRedis(
            host=host, port=port, db=redis_db, decode_responses=True
        )
        publisher_store.get("test")
        socketio = SocketIO(message_queue=redis_url)
    except redis.ConnectionError:
        pass

    return socketio 
Example 9
Project: iSDX   Author: sdn-ixp   File: replay.py    License: Apache License 2.0 5 votes vote down vote up
def __init__(self, channel, address, port):
        self.redis_client = redis.StrictRedis(host=address, port=port)
        self.channel = channel 
Example 10
Project: friendly-telegram   Author: friendly-telegram   File: raphielgang.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def REDIS(self):
        return redis.StrictRedis(host="localhost", port=6379, db=0) 
Example 11
Project: fishroom   Author: tuna   File: db.py    License: GNU General Public License v3.0 5 votes vote down vote up
def get_redis():
    if 'redis' not in __dbctx:
        if config['redis'].get('unix_socket_path') is not None:
            redis_client = redis.StrictRedis(
                unix_socket_path=config['redis']['unix_socket_path'])
        else:
            redis_client = redis.StrictRedis(
                host=config['redis']['host'], port=config['redis']['port'])

        __dbctx['redis'] = redis_client
    return __dbctx['redis']


# vim: ts=4 sw=4 sts=4 expandtab 
Example 12
Project: trader   Author: BigBrotherTrade   File: __init__.py    License: Apache License 2.0 5 votes vote down vote up
def is_trading_day(day: datetime.datetime):
    s = redis.StrictRedis(
        host=config.get('REDIS', 'host', fallback='localhost'),
        db=config.getint('REDIS', 'db', fallback=1), decode_responses=True)
    return day, day.strftime('%Y%m%d') in (s.get('TradingDay'), s.get('LastTradingDay'))
    # async with aiohttp.ClientSession() as session:
    #     await max_conn_cffex.acquire()
    #     async with session.get(
    #             'http://{}/fzjy/mrhq/{}/index.xml'.format(cffex_ip, day.strftime('%Y%m/%d')),
    #             allow_redirects=False) as response:
    #         max_conn_cffex.release()
    #         return day, response.status != 302 
Example 13
Project: trader   Author: BigBrotherTrade   File: test_api.py    License: Apache License 2.0 5 votes vote down vote up
def setUp(self):
        self.redis_client = redis.StrictRedis(
            host=config.get('REDIS', 'host', fallback='localhost'),
            db=config.getint('REDIS', 'db', fallback=1), decode_responses=True)
        self.api = CTPTrader(io_loop=self.loop) 
Example 14
Project: evolution-strategies-starter   Author: openai   File: dist.py    License: MIT License 5 votes vote down vote up
def retry_connect(redis_cfg, tries=300, base_delay=4.):
    for i in range(tries):
        try:
            r = redis.StrictRedis(**redis_cfg)
            r.ping()
            return r
        except redis.ConnectionError as e:
            if i == tries - 1:
                raise
            else:
                delay = base_delay * (1 + (os.getpid() % 10) / 9)
                logger.warning('Could not connect to {}. Retrying after {:.2f} sec ({}/{}). Error: {}'.format(
                    redis_cfg, delay, i + 2, tries, e))
                time.sleep(delay) 
Example 15
Project: rlite-py   Author: seppo0010   File: patch.py    License: BSD 2-Clause "Simplified" License 5 votes vote down vote up
def test_basic(self):
        r = redis.StrictRedis()
        r.set('foo', 'bar')
        self.assertEquals(b'bar', r.get('foo')) 
Example 16
Project: rlite-py   Author: seppo0010   File: patch.py    License: BSD 2-Clause "Simplified" License 5 votes vote down vote up
def test_basic_string(self):
        r = redis.StrictRedis(decode_responses=True)
        r.set('foo', 'bar')
        self.assertEquals('bar', r.get('foo')) 
Example 17
Project: rlite-py   Author: seppo0010   File: patch.py    License: BSD 2-Clause "Simplified" License 5 votes vote down vote up
def test_from_url(self):
        r = redis.StrictRedis.from_url('redis://localhost/1',
                                       decode_responses=True)

        r.set('foo', 'bar')
        self.assertEquals('bar', r.get('foo')) 
Example 18
Project: rlite-py   Author: seppo0010   File: patch.py    License: BSD 2-Clause "Simplified" License 5 votes vote down vote up
def test_keys(self):
        r = redis.StrictRedis(decode_responses=True)

        r.set('abc', 'foo')
        r.zadd('zset', 0, 'text')
        r.hset('hset', 'foo', 'bar')

        res = r.keys('*')

        self.assertEquals(set(['abc', 'zset', 'hset']), set(res)) 
Example 19
Project: rlite-py   Author: seppo0010   File: patch.py    License: BSD 2-Clause "Simplified" License 5 votes vote down vote up
def test_scan(self):
        r = redis.StrictRedis(decode_responses=True)

        r.set('abc', 'foo')
        r.zadd('zset', 0, 'text')
        r.hset('hset', 'foo', 'bar')

        res = list(r.scan_iter(match='*set'))

        self.assertEquals(set(['zset', 'hset']), set(res)) 
Example 20
Project: pyspider   Author: binux   File: taskdb.py    License: Apache License 2.0 5 votes vote down vote up
def __init__(self, host='localhost', port=6379, db=0):
        self.redis = redis.StrictRedis(host=host, port=port, db=db)
        try:
            self.redis.scan(count=1)
            self.scan_available = True
        except Exception as e:
            logging.debug("redis_scan disabled: %r", e)
            self.scan_available = False 
Example 21
Project: watchdog   Author: flipkart-incubator   File: Config.py    License: Apache License 2.0 5 votes vote down vote up
def getRedisVendorConnection(cls):
        redisHost = cls.getRedisHost()
        redisPort = cls.getRedisPort()
        redisDB = cls.readSetting("Redis", "VendorsDB", cls.default['redisVendorDB'])
        return redis.StrictRedis(host=redisHost, port=redisPort, db=redisDB, charset='utf-8', decode_responses=True) 
Example 22
Project: watchdog   Author: flipkart-incubator   File: Config.py    License: Apache License 2.0 5 votes vote down vote up
def getRedisNotificationsConnection(cls):
        redisHost = cls.getRedisHost()
        redisPort = cls.getRedisPort()
        redisDB = cls.readSetting("Redis", "NotificationsDB", cls.default['redisNotificationsDB'])
        return redis.StrictRedis(host=redisHost, port=redisPort, db=redisDB, charset="utf-8", decode_responses=True) 
Example 23
Project: watchdog   Author: flipkart-incubator   File: Config.py    License: Apache License 2.0 5 votes vote down vote up
def getRedisRefConnection(cls):
        redisHost = cls.getRedisHost()
        redisPort = cls.getRedisPort()
        redisDB = cls.readSetting("Redis", "RefDB", cls.default['redisRefDB'])
        return redis.StrictRedis(host=redisHost, port=redisPort, db=redisDB, charset="utf-8", decode_responses=True)

    # Flask 
Example 24
Project: haipproxy   Author: SpiderClub   File: redis_util.py    License: MIT License 5 votes vote down vote up
def get_redis_conn(**kwargs):
    host = kwargs.get('host', REDIS_HOST)
    port = kwargs.get('port', REDIS_PORT)
    db = kwargs.get('db', REDIS_DB)
    password = kwargs.get('password', REDIS_PASSWORD)
    return redis.StrictRedis(host, port, db, password) 
Example 25
Project: seqr   Author: macarthur-lab   File: redis_utils.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def safe_redis_get_json(cache_key):
    try:
        redis_client = redis.StrictRedis(host=REDIS_SERVICE_HOSTNAME, socket_connect_timeout=3)
        value = redis_client.get(cache_key)
        if value:
            logger.info('Loaded {} from redis'.format(cache_key))
            return json.loads(value)
    except ValueError as e:
        logger.warn('Unable to fetch "{}" from redis:\t{}'.format(cache_key, str(e)))
    except Exception as e:
        logger.warn('Unable to connect to redis host {}: {}'.format(REDIS_SERVICE_HOSTNAME, str(e)))
    return None 
Example 26
Project: seqr   Author: macarthur-lab   File: redis_utils.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def safe_redis_set_json(cache_key, value):
    try:
        redis_client = redis.StrictRedis(host=REDIS_SERVICE_HOSTNAME, socket_connect_timeout=3)
        redis_client.set(cache_key, json.dumps(value))
    except Exception as e:
        logger.warn('Unable to write to redis host {}: {}'.format(REDIS_SERVICE_HOSTNAME, str(e))) 
Example 27
Project: mquery   Author: CERT-Polska   File: db.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def __init__(self, redis_host: str, redis_port: int) -> None:
        self.redis = StrictRedis(
            host=redis_host, port=redis_port, decode_responses=True
        ) 
Example 28
Project: git-pandas   Author: wdm0006   File: cache.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __init__(self, host='localhost', port=6379, db=12, max_keys=1000, ttl=None, **kwargs):
        if not _HAS_REDIS:
            raise ImportError('Need redis installed to use redis cache')
        self._cache = redis.StrictRedis(host=host, port=port, db=db, **kwargs)
        self._key_list = []
        self._max_keys = max_keys
        self.ttl = ttl
        self.prefix = 'gitpandas_'

        # sync with any keys that already exist in this database (order will not be preserved)
        self.sync() 
Example 29
Project: pagure   Author: Pagure   File: query.py    License: GNU General Public License v2.0 5 votes vote down vote up
def set_redis(host, port, dbname):
    """ Set the redis connection with the specified information. """
    global REDIS
    pool = redis.ConnectionPool(host=host, port=port, db=dbname)
    REDIS = redis.StrictRedis(connection_pool=pool) 
Example 30
Project: distributed_rl   Author: neka-nat   File: replay.py    License: MIT License 5 votes vote down vote up
def __init__(self, size=50000, connect=redis.StrictRedis(host='localhost'),
                 use_compress=False):
        super(Replay, self).__init__()
        self.setDaemon(True)
        self._memory = replay_memory.PrioritizedMemory(size, use_compress)
        self._connect = connect
        self._connect.delete('experience')
        self._lock = threading.Lock()