python使用异步redis--aioredis
Posted 帝壹
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python使用异步redis--aioredis相关的知识,希望对你有一定的参考价值。
安装
最简单的安装方式:
pip install aioredis
需要先安装的依赖:
- Python 3.3 and asyncio 或 Python 3.4+
- hiredis
例子
simple low-level interface:
import asyncio
import aioredis
loop = asyncio.get_event_loop()
async def go():
conn = await aioredis.create_connection(
('localhost', 6379), loop=loop)
await conn.execute('set', 'my-key', 'value')
val = await conn.execute('get', 'my-key')
print(val)
conn.close()
await conn.wait_closed()
loop.run_until_complete(go())
# will print 'value'
simple high-level interface:
import asyncio
import aioredis
loop = asyncio.get_event_loop()
async def go():
redis = await aioredis.create_redis(
('localhost', 6379), loop=loop)
await redis.set('my-key', 'value')
val = await redis.get('my-key')
print(val)
redis.close()
await redis.wait_closed()
loop.run_until_complete(go())
# will print 'value'
Connections pool:
import asyncio
import aioredis
loop = asyncio.get_event_loop()
async def go():
pool = await aioredis.create_pool(
('localhost', 6379),
minsize=5, maxsize=10,
loop=loop)
with await pool as redis: # high-level redis API instance
await redis.set('my-key', 'value')
print(await redis.get('my-key'))
# graceful shutdown
pool.close()
await pool.wait_closed()
loop.run_until_complete(go())
以上是关于python使用异步redis--aioredis的主要内容,如果未能解决你的问题,请参考以下文章