Python aioredis.create_redis() Examples

The following are 30 code examples of aioredis.create_redis(). 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 also want to check out all available functions/classes of the module aioredis , or try the search function .
Example #1
Source File: __init__.py    From trader with 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
Source File: __init__.py    From vitimas-da-intolerancia with GNU Lesser General Public License v3.0 6 votes vote down vote up
def home(request):
    redis = await create_redis(REDIS_URL)
    cases = await redis.get("cases")

    if cases:
        cases = pickle.loads(cases)
    else:
        data = Data()
        cases = await data.cases()
        await redis.set("cases", pickle.dumps(cases))

    redis.close()
    await redis.wait_closed()

    return {
        "cases": cases,
        "title": TITLE,
        "url_path": "/",
        "max_chars": CASE_MAX_CHARS,
    } 
Example #3
Source File: etcd.py    From backend.ai-manager with GNU Lesser General Public License v3.0 6 votes vote down vote up
def config_ctx(cli_ctx):
    config = cli_ctx.config
    ctx = {}
    ctx['config'] = config
    # scope_prefix_map is created inside ConfigServer
    config_server = ConfigServer(
        ctx, config['etcd']['addr'],
        config['etcd']['user'], config['etcd']['password'],
        config['etcd']['namespace'])
    raw_redis_config = await config_server.etcd.get_prefix('config/redis')
    config['redis'] = redis_config_iv.check(raw_redis_config)
    ctx['redis_image'] = await aioredis.create_redis(
        config['redis']['addr'].as_sockaddr(),
        password=config['redis']['password'] if config['redis']['password'] else None,
        timeout=3.0,
        encoding='utf8',
        db=REDIS_IMAGE_DB)
    try:
        yield config_server
    finally:
        ctx['redis_image'].close()
        await ctx['redis_image'].wait_closed()
        await config_server.close() 
Example #4
Source File: asyncio_redis_manager.py    From python-socketio with MIT License 6 votes vote down vote up
def _publish(self, data):
        retry = True
        while True:
            try:
                if self.pub is None:
                    self.pub = await aioredis.create_redis(
                        (self.host, self.port), db=self.db,
                        password=self.password, ssl=self.ssl
                    )
                return await self.pub.publish(self.channel,
                                              pickle.dumps(data))
            except (aioredis.RedisError, OSError):
                if retry:
                    self._get_logger().error('Cannot publish to redis... '
                                             'retrying')
                    self.pub = None
                    retry = False
                else:
                    self._get_logger().error('Cannot publish to redis... '
                                             'giving up')
                    break 
Example #5
Source File: asyncio_redis_manager.py    From python-socketio with MIT License 6 votes vote down vote up
def _listen(self):
        retry_sleep = 1
        while True:
            try:
                if self.sub is None:
                    self.sub = await aioredis.create_redis(
                        (self.host, self.port), db=self.db,
                        password=self.password, ssl=self.ssl
                    )
                self.ch = (await self.sub.subscribe(self.channel))[0]
                return await self.ch.get()
            except (aioredis.RedisError, OSError):
                self._get_logger().error('Cannot receive from redis... '
                                         'retrying in '
                                         '{} secs'.format(retry_sleep))
                self.sub = None
                await asyncio.sleep(retry_sleep)
                retry_sleep *= 2
                if retry_sleep > 60:
                    retry_sleep = 60 
Example #6
Source File: pubsub.py    From aioredis with MIT License 6 votes vote down vote up
def main():
    pub = await aioredis.create_redis(
        'redis://localhost')
    sub = await aioredis.create_redis(
        'redis://localhost')
    res = await sub.subscribe('chan:1')
    ch1 = res[0]

    tsk = asyncio.ensure_future(reader(ch1))

    res = await pub.publish_json('chan:1', ["Hello", "world"])
    assert res == 1

    await sub.unsubscribe('chan:1')
    await tsk
    sub.close()
    pub.close() 
Example #7
Source File: test_log.py    From QuLab with MIT License 6 votes vote down vote up
def test_redis_handler(event_loop):
    import redis
    r = redis.Redis()
    logger = logging.getLogger('qulabtest2')
    logger.setLevel(logging.DEBUG)
    handler = RedisHandler(r)
    handler.setLevel(logging.DEBUG)
    logger.addHandler(handler)

    sub = await aioredis.create_redis('redis://localhost')
    logChannel, = await sub.subscribe('log')

    logger.debug('hello')

    if await logChannel.wait_message():
        bmsg = await logChannel.get()
        record = logging.makeLogRecord(unpack(bmsg))
        assert record.msg == 'hello'
    pass 
Example #8
Source File: progress_bar.py    From quart with MIT License 6 votes vote down vote up
def start_work():
    global aredis
    loop = asyncio.get_event_loop()
    aredis = await aioredis.create_redis('redis://localhost', loop=loop)

    if await aredis.get('state') == b'running':
        return "<center>Please wait for current work to finish.</center>"
    else:
        await aredis.set('state', 'ready')

    if await aredis.get('state') == b'ready':
        loop.create_task(some_work())
        body = '''
        <center>
        work started!
        </center>
        <script type="text/javascript">
            window.location = "''' + url_for('progress') + '''";
        </script>'''
        return body 
Example #9
Source File: conftest.py    From lightbus with Apache License 2.0 5 votes vote down vote up
def create_redis_client(_closable, redis_server_config, loop, request):
    """Wrapper around aioredis.create_redis."""

    async def f(*args, **kw):
        kwargs = {}
        kwargs.update(redis_server_config)
        kwargs.update(kw)
        redis = await aioredis.create_redis(*args, **kwargs)
        _closable(redis)
        return redis

    return f 
Example #10
Source File: conftest.py    From lightbus with Apache License 2.0 5 votes vote down vote up
def redis_server_b_url():
    url = os.environ.get("REDIS_URL_B", "") or "redis://127.0.0.1:6379/11"
    redis = await aioredis.create_redis(url)
    await redis.flushdb()
    redis.close()
    return url 
Example #11
Source File: conftest.py    From lightbus with Apache License 2.0 5 votes vote down vote up
def redis_server_url():
    # We use 127.0.0.1 rather than 'localhost' as this bypasses some
    # problems we encounter with getaddrinfo when performing tests
    # which create a lot of connections (e.g. the reliability tests)
    url = os.environ.get("REDIS_URL", "") or "redis://127.0.0.1:6379/10"
    redis = await aioredis.create_redis(url)
    await redis.flushdb()
    redis.close()
    return url 
Example #12
Source File: app.py    From pastey with MIT License 5 votes vote down vote up
def init_app(app):
    app['db'] = await aioredis.create_redis(
        address=(
            os.getenv('REDIS_HOST', 'redis'),
            int(os.getenv('REDIS_PORT', 6379))
        ),
        db=int(os.getenv('REDIS_DB', 1)),
        loop=None,
        encoding='utf-8',
    )
    # flush the DB (not very production safe):
    await app['db'].flushdb()
    return app 
Example #13
Source File: keystore.py    From py-walletconnect-bridge with GNU Lesser General Public License v3.0 5 votes vote down vote up
def create_connection(event_loop, host='localhost', port=6379, db=0):
  redis_uri = 'redis://{}:{}/{}'.format(host, port, db)
  return await aioredis.create_redis(address=redis_uri, db=db,
                                     encoding='utf-8', loop=event_loop) 
Example #14
Source File: virtual_printer_wrapper.py    From inshack-2018 with GNU General Public License v3.0 5 votes vote down vote up
def __load_secret(self):
        key = 'vps-' + self.__remote_ip()
        redis = await aioredis.create_redis(self.redis_addr, timeout=REDIS_TIMEOUT)
        secret = await redis.get(key)
        redis.close()
        access_log.info('retrieving: ({}, {})'.format(key, secret))
        return secret
    ##
    ## @brief      { function_description }
    ## 
Example #15
Source File: virtual_printer_wrapper.py    From inshack-2018 with GNU General Public License v3.0 5 votes vote down vote up
def __save_secret(self, secret):
        key = 'vps-' + self.__remote_ip()
        redis = await aioredis.create_redis(self.redis_addr, timeout=REDIS_TIMEOUT)
        access_log.info('adding: ({}, {})'.format(key, secret))
        await redis.set(key, secret, expire=30) # register secret
        redis.close()
    ##
    ## @brief      { function_description }
    ## 
Example #16
Source File: application.py    From politicos with GNU Affero General Public License v3.0 5 votes vote down vote up
def init_with_loop(self, loop):
        self.redis = loop.run_until_complete(
            aioredis.create_redis(
                (options.redis_host, options.redis_port),
                loop=loop
            )
        )
        self.cache = RedisCacheBackend(self.redis)
        es_hosts = [x.strip() for x in options.es_hosts.split(',')]
        self.es = AsyncElasticsearch(hosts=es_hosts, loop=loop) 
Example #17
Source File: caching.py    From apex-sigma-core with GNU General Public License v3.0 5 votes vote down vote up
def init(self):
        """
        Initializes any potential asyncronous tasks required
        by the Cacher inheriting child.
        :return:
        """
        self.conn = await aioredis.create_redis(f'redis://{self.cfg.host}:{self.cfg.port}/{self.cfg.db}') 
Example #18
Source File: aiourpc.py    From urpc with The Unlicense 5 votes vote down vote up
def connect(self):
        '''connect to Redis server using options from init.
        You should not use it direct normally, main loop and messages operations call it anyway.'''
        if not self._connect:
            self._connect = await aioredis.create_redis(
                'redis://' + self.config['host'],
                db=int(self.config['db']))
            logging.debug("Connecting to " + self.config['host'] + "/" + str(self.config['db']))
        return self._connect 
Example #19
Source File: __init__.py    From vitimas-da-intolerancia with GNU Lesser General Public License v3.0 5 votes vote down vote up
def clear_cache():
    redis = await create_redis(REDIS_URL)
    keys = len(await redis.keys("*"))
    print(f"Flushing {keys} key/value pair(s)")
    await redis.flushall()
    redis.close()
    await redis.wait_closed()
    print("Done :)") 
Example #20
Source File: utils.py    From browsertrix with Apache License 2.0 5 votes vote down vote up
def init_redis(redis_url: str, loop: AbstractEventLoop) -> Redis:
    return await create_redis(redis_url, encoding='utf-8', loop=loop) 
Example #21
Source File: database.py    From mee6 with MIT License 5 votes vote down vote up
def create(self):
        self.redis = await aioredis.create_redis(
            self.redis_address,
            encoding='utf8'
        ) 
Example #22
Source File: gateway_bot.py    From mee6 with MIT License 5 votes vote down vote up
def __broker_connect(self):
        self.broker = await aioredis.create_redis(
            parse_redis_url(self.broker_url),
            encoding='utf8'
        ) 
Example #23
Source File: comparison.py    From aredis with MIT License 5 votes vote down vote up
def test_aioredis(i, loop):
    start = time.time()
    redis = await aioredis.create_redis((HOST, 6379), loop=loop)
    val = None
    for i in range(i):
        val = await redis.keys('*')
    print(time.time() - start)
    redis.close()
    await redis.wait_closed()
    return val 
Example #24
Source File: brother2.py    From trader with Apache License 2.0 5 votes vote down vote up
def query(self, query_type: str, **kwargs):
        sub_client = None
        channel_name1, channel_name2 = None, None
        try:
            sub_client = await aioredis.create_redis(
                (config.get('REDIS', 'host', fallback='localhost'),
                 config.getint('REDIS', 'port', fallback=6379)),
                db=config.getint('REDIS', 'db', fallback=1))
            request_id = self.next_id()
            kwargs['RequestID'] = request_id
            channel_name1 = self.__trade_response_format.format('OnRspQry' + query_type, request_id)
            channel_name2 = self.__trade_response_format.format('OnRspError', request_id)
            ch1, ch2 = await sub_client.psubscribe(channel_name1, channel_name2)
            cb = self.io_loop.create_future()
            tasks = [
                asyncio.ensure_future(self.query_reader(ch1, cb), loop=self.io_loop),
                asyncio.ensure_future(self.query_reader(ch2, cb), loop=self.io_loop),
            ]
            self.redis_client.publish(self.__request_format.format('ReqQry' + query_type), json.dumps(kwargs))
            rst = await asyncio.wait_for(cb, HANDLER_TIME_OUT, loop=self.io_loop)
            await sub_client.punsubscribe(channel_name1, channel_name2)
            sub_client.close()
            await asyncio.wait(tasks, loop=self.io_loop)
            return rst
        except Exception as e:
            logger.error('%s failed: %s', query_type, repr(e), exc_info=True)
            if sub_client and sub_client.in_pubsub and channel_name1:
                await sub_client.unsubscribe(channel_name1, channel_name2)
                sub_client.close()
            return None 
Example #25
Source File: brother2.py    From trader with Apache License 2.0 5 votes vote down vote up
def SubscribeMarketData(self, inst_ids: list):
        sub_client = None
        channel_name1, channel_name2 = None, None
        try:
            sub_client = await aioredis.create_redis(
                (config.get('REDIS', 'host', fallback='localhost'),
                 config.getint('REDIS', 'port', fallback=6379)),
                db=config.getint('REDIS', 'db', fallback=1))
            channel_name1 = self.__market_response_format.format('OnRspSubMarketData', 0)
            channel_name2 = self.__market_response_format.format('OnRspError', 0)
            ch1, ch2 = await sub_client.psubscribe(channel_name1, channel_name2)
            cb = self.io_loop.create_future()
            tasks = [
                asyncio.ensure_future(self.query_reader(ch1, cb), loop=self.io_loop),
                asyncio.ensure_future(self.query_reader(ch2, cb), loop=self.io_loop),
            ]
            self.redis_client.publish(self.__request_format.format('SubscribeMarketData'), json.dumps(inst_ids))
            rst = await asyncio.wait_for(cb, HANDLER_TIME_OUT, loop=self.io_loop)
            await sub_client.punsubscribe(channel_name1, channel_name2)
            sub_client.close()
            await asyncio.wait(tasks, loop=self.io_loop)
            return rst
        except Exception as e:
            logger.error('SubscribeMarketData failed: %s', repr(e), exc_info=True)
            if sub_client and sub_client.in_pubsub and channel_name1:
                await sub_client.unsubscribe(channel_name1, channel_name2)
                sub_client.close()
            return None 
Example #26
Source File: brother2.py    From trader with Apache License 2.0 5 votes vote down vote up
def UnSubscribeMarketData(self, inst_ids: list):
        sub_client = None
        channel_name1, channel_name2 = None, None
        try:
            sub_client = await aioredis.create_redis(
                (config.get('REDIS', 'host', fallback='localhost'),
                 config.getint('REDIS', 'port', fallback=6379)),
                db=config.getint('REDIS', 'db', fallback=1))
            channel_name1 = self.__market_response_format.format('OnRspUnSubMarketData', 0)
            channel_name2 = self.__market_response_format.format('OnRspError', 0)
            ch1, ch2 = await sub_client.psubscribe(channel_name1, channel_name2)
            cb = self.io_loop.create_future()
            tasks = [
                asyncio.ensure_future(self.query_reader(ch1, cb), loop=self.io_loop),
                asyncio.ensure_future(self.query_reader(ch2, cb), loop=self.io_loop),
            ]
            self.redis_client.publish(self.__request_format.format('UnSubscribeMarketData'), json.dumps(inst_ids))
            rst = await asyncio.wait_for(cb, HANDLER_TIME_OUT, loop=self.io_loop)
            await sub_client.punsubscribe(channel_name1, channel_name2)
            sub_client.close()
            await asyncio.wait(tasks, loop=self.io_loop)
            return rst
        except Exception as e:
            logger.error('SubscribeMarketData failed: %s', repr(e), exc_info=True)
            if sub_client and sub_client.in_pubsub and channel_name1:
                await sub_client.unsubscribe(channel_name1, channel_name2)
                sub_client.close()
            return None 
Example #27
Source File: transaction.py    From aioredis with MIT License 5 votes vote down vote up
def main():
    redis = await aioredis.create_redis(
        'redis://localhost')
    await redis.delete('foo', 'bar')
    tr = redis.multi_exec()
    fut1 = tr.incr('foo')
    fut2 = tr.incr('bar')
    res = await tr.execute()
    res2 = await asyncio.gather(fut1, fut2)
    print(res)
    assert res == res2

    redis.close()
    await redis.wait_closed() 
Example #28
Source File: transaction2.py    From aioredis with MIT License 5 votes vote down vote up
def main():
    redis = await aioredis.create_redis(
        'redis://localhost')

    async def transaction():
        tr = redis.multi_exec()
        future1 = tr.set('foo', '123')
        future2 = tr.set('bar', '321')
        result = await tr.execute()
        assert result == await asyncio.gather(future1, future2)
        return result

    await transaction()
    redis.close()
    await redis.wait_closed() 
Example #29
Source File: scan.py    From aioredis with MIT License 5 votes vote down vote up
def main():
    """Scan command example."""
    redis = await aioredis.create_redis(
        'redis://localhost')

    await redis.mset('key:1', 'value1', 'key:2', 'value2')
    cur = b'0'  # set initial cursor to 0
    while cur:
        cur, keys = await redis.scan(cur, match='key:*')
        print("Iteration results:", keys)

    redis.close()
    await redis.wait_closed() 
Example #30
Source File: commands.py    From aioredis with MIT License 5 votes vote down vote up
def main():
    # Redis client bound to single connection (no auto reconnection).
    redis = await aioredis.create_redis(
        'redis://localhost')
    await redis.set('my-key', 'value')
    val = await redis.get('my-key')
    print(val)

    # gracefully closing underlying connection
    redis.close()
    await redis.wait_closed()