本文整理汇总了Python中aioredis.Redis方法的典型用法代码示例。如果您正苦于以下问题:Python aioredis.Redis方法的具体用法?Python aioredis.Redis怎么用?Python aioredis.Redis使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类aioredis
的用法示例。
在下文中一共展示了aioredis.Redis方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _create_redis_session
# 需要导入模块: import aioredis [as 别名]
# 或者: from aioredis import Redis [as 别名]
def _create_redis_session(self) -> None:
"""
Create the Redis connection pool, and then open the redis event gate.
If constants.Redis.use_fakeredis is True, we'll set up a fake redis pool instead
of attempting to communicate with a real Redis server. This is useful because it
means contributors don't necessarily need to get Redis running locally just
to run the bot.
The fakeredis cache won't have persistence across restarts, but that
usually won't matter for local bot testing.
"""
if constants.Redis.use_fakeredis:
log.info("Using fakeredis instead of communicating with a real Redis server.")
self.redis_session = await fakeredis.aioredis.create_redis_pool()
else:
self.redis_session = await aioredis.create_redis_pool(
address=(constants.Redis.host, constants.Redis.port),
password=constants.Redis.password,
)
self.redis_closed = False
self.redis_ready.set()
示例2: init
# 需要导入模块: import aioredis [as 别名]
# 或者: from aioredis import Redis [as 别名]
def init():
"""Create a connection to the Redis server."""
global redis_conn
conn = await aioredis.create_connection(
"redis://{}:{}".format(
SETTINGS.get("FLOW_EXECUTOR", {})
.get("REDIS_CONNECTION", {})
.get("host", "localhost"),
SETTINGS.get("FLOW_EXECUTOR", {})
.get("REDIS_CONNECTION", {})
.get("port", 56379),
),
db=int(
SETTINGS.get("FLOW_EXECUTOR", {}).get("REDIS_CONNECTION", {}).get("db", 1)
),
)
redis_conn = aioredis.Redis(conn)
示例3: __init__
# 需要导入模块: import aioredis [as 别名]
# 或者: from aioredis import Redis [as 别名]
def __init__(self, connection):
"""
Redis instance constructor
Constructor takes single argument - a redis host address
The address can be one of the following:
* a dict - {'host': 'localhost', 'port': 6379,
'db': 0, 'password': 'pass'}
all keys except host and port will be passed as kwargs to
the aioredis.create_redis_pool();
* a Redis URI - "redis://host:6379/0?encoding=utf-8";
* a (host, port) tuple - ('localhost', 6379);
* or a unix domain socket path string - "/path/to/redis.sock".
* a redis connection pool.
:param connection: redis host address (dict, tuple or str)
"""
self.connection = connection
self._pool = None
self._lock = asyncio.Lock()
self.set_lock_script = re.sub(r'^\s+', '', self.SET_LOCK_SCRIPT, flags=re.M).strip()
self.unset_lock_script = re.sub(r'^\s+', '', self.UNSET_LOCK_SCRIPT, flags=re.M).strip()
示例4: __init__
# 需要导入模块: import aioredis [as 别名]
# 或者: from aioredis import Redis [as 别名]
def __init__(self, *args, **kwargs):
if "connector" in kwargs:
warnings.warn(
"If login() is called (or the bot is started), the connector will be overwritten "
"with an internal one"
)
super().__init__(*args, **kwargs)
self.http_session: Optional[aiohttp.ClientSession] = None
self.redis_session: Optional[aioredis.Redis] = None
self.redis_ready = asyncio.Event()
self.redis_closed = False
self.api_client = api.APIClient(loop=self.loop)
self._connector = None
self._resolver = None
self._guild_available = asyncio.Event()
statsd_url = constants.Stats.statsd_host
if DEBUG_MODE:
# Since statsd is UDP, there are no errors for sending to a down port.
# For this reason, setting the statsd host to 127.0.0.1 for development
# will effectively disable stats.
statsd_url = "127.0.0.1"
self.stats = AsyncStatsClient(self.loop, statsd_url, 8125, prefix="bot")
示例5: _recreate
# 需要导入模块: import aioredis [as 别名]
# 或者: from aioredis import Redis [as 别名]
def _recreate(self) -> None:
"""Re-create the connector, aiohttp session, the APIClient and the Redis session."""
# Use asyncio for DNS resolution instead of threads so threads aren't spammed.
# Doesn't seem to have any state with regards to being closed, so no need to worry?
self._resolver = aiohttp.AsyncResolver()
# Its __del__ does send a warning but it doesn't always show up for some reason.
if self._connector and not self._connector._closed:
log.warning(
"The previous connector was not closed; it will remain open and be overwritten"
)
if self.redis_session and not self.redis_session.closed:
log.warning(
"The previous redis pool was not closed; it will remain open and be overwritten"
)
# Create the redis session
self.loop.create_task(self._create_redis_session())
# Use AF_INET as its socket family to prevent HTTPS related problems both locally
# and in production.
self._connector = aiohttp.TCPConnector(
resolver=self._resolver,
family=socket.AF_INET,
)
# Client.login() will call HTTPClient.static_login() which will create a session using
# this connector attribute.
self.http.connector = self._connector
# Its __del__ does send a warning but it doesn't always show up for some reason.
if self.http_session and not self.http_session.closed:
log.warning(
"The previous session was not closed; it will remain open and be overwritten"
)
self.http_session = aiohttp.ClientSession(connector=self._connector)
self.api_client.recreate(force=True, connector=self._connector)
示例6: test_repr
# 需要导入模块: import aioredis [as 别名]
# 或者: from aioredis import Redis [as 别名]
def test_repr(create_redis, server):
redis = await create_redis(server.tcp_address, db=1)
assert repr(redis) in {
'<Redis <RedisConnection [db:1]>>',
'<Redis <ConnectionsPool [db:1, size:[1:10], free:1]>>',
}
redis = await create_redis(server.tcp_address, db=0)
assert repr(redis) in {
'<Redis <RedisConnection [db:0]>>',
'<Redis <ConnectionsPool [db:0, size:[1:10], free:1]>>',
}
示例7: test_yield_from_backwards_compatibility
# 需要导入模块: import aioredis [as 别名]
# 或者: from aioredis import Redis [as 别名]
def test_yield_from_backwards_compatibility(create_redis, server):
redis = await create_redis(server.tcp_address)
assert isinstance(redis, Redis)
# TODO: there should not be warning
# with pytest.warns(UserWarning):
with await redis as client:
assert isinstance(client, Redis)
assert client is not redis
assert await client.ping()
示例8: deinit
# 需要导入模块: import aioredis [as 别名]
# 或者: from aioredis import Redis [as 别名]
def deinit():
"""Close the Redis connection cleanly."""
redis_conn.close()
await redis_conn.wait_closed()
示例9: __init__
# 需要导入模块: import aioredis [as 别名]
# 或者: from aioredis import Redis [as 别名]
def __init__(self, redis: Redis, key: str, timeout: int = 30, wait_timeout: int = 30, *, token: str = None):
self.redis = redis
self.key = key
self.timeout = timeout
self.wait_timeout = wait_timeout # Can be None to wait forever
self._token = token or str(uuid.uuid4())
示例10: start
# 需要导入模块: import aioredis [as 别名]
# 或者: from aioredis import Redis [as 别名]
def start(self, *args, **kwargs):
t = threading.Thread(target=block_check, args=(self.loop,))
t.setDaemon(True)
t.start()
self.redis = aioredis.Redis(await aioredis.create_pool("redis://" + self.config.redis_host))
# self.loop.create_task(self._shards_reader())
return await super().start(*args, **kwargs)
示例11: log_redis_info
# 需要导入模块: import aioredis [as 别名]
# 或者: from aioredis import Redis [as 别名]
def log_redis_info(redis: Redis, log_func: Callable[[str], Any]) -> None:
with await redis as r:
info, key_count = await asyncio.gather(r.info(), r.dbsize())
log_func(
f'redis_version={info["server"]["redis_version"]} '
f'mem_usage={info["memory"]["used_memory_human"]} '
f'clients_connected={info["clients"]["connected_clients"]} '
f'db_keys={key_count}'
)
示例12: __init__
# 需要导入模块: import aioredis [as 别名]
# 或者: from aioredis import Redis [as 别名]
def __init__(
self,
job_id: str,
redis: Redis,
_queue_name: str = default_queue_name,
_deserializer: Optional[Deserializer] = None,
):
self.job_id = job_id
self._redis = redis
self._queue_name = _queue_name
self._deserializer = _deserializer
示例13: init_redis
# 需要导入模块: import aioredis [as 别名]
# 或者: from aioredis import Redis [as 别名]
def init_redis(redis_url: str, loop: AbstractEventLoop) -> Redis:
return await create_redis(redis_url, encoding='utf-8', loop=loop)
示例14: __init__
# 需要导入模块: import aioredis [as 别名]
# 或者: from aioredis import Redis [as 别名]
def __init__(self) -> None:
self.redis: Redis = None
self.session: ClientSession = None
self.loop: AbstractEventLoop = None
self.depth: int = env('DEFAULT_DEPTH', type_=int, default=1)
self.same_domain_depth: int = env(
'DEFAULT_SAME_DOMAIN_DEPTH', type_=int, default=100
)
self.num_browsers: int = env('DEFAULT_NUM_BROWSERS', type_=int, default=2)
self.flock: str = env('DEFAULT_FLOCK', default='browsers')
self.shepherd_host: str = env(
'DEFAULT_SHEPHERD', default='http://shepherd:9020'
)
self.browser_api_url: str = f'{self.shepherd_host}/api'
self.pool: str = env('DEFAULT_POOL', default='')
self.scan_key: str = 'a:*:info'
self.container_environ: Dict[str, str] = {
'URL': 'about:blank',
'REDIS_URL': env('REDIS_URL', default=DEFAULT_REDIS_URL),
'WAIT_FOR_Q': '10',
'TAB_TYPE': 'CrawlerTab',
'CRAWL_NO_NETCACHE': '0',
'VNC_PASS': 'pass',
'IDLE_TIMEOUT': '',
'BEHAVIOR_API_URL': 'http://behaviors:3030',
'SCREENSHOT_API_URL': env('SCREENSHOT_API_URL'),
'EXTRACTED_RAW_DOM_API_URL': env('EXTRACTED_RAW_DOM_API_URL'),
}
self.default_browser = None
if os.environ.get('DEBUG'):
logger.setLevel(logging.DEBUG)
else:
logger.setLevel(logging.INFO)
示例15: redis
# 需要导入模块: import aioredis [as 别名]
# 或者: from aioredis import Redis [as 别名]
def redis(self) -> Redis:
"""Retrieve the redis instance of the crawl manager
:return: The redis instance of the crawl manager
"""
return self.manager.redis