aioredis 是基于 Asyncio 实现的异步 Redis 的 Python 开发包。
要求:
Python 3.3+
asyncio or Python 3.4+
hiredis
示例代码:
import asyncio import aioredis loop = asyncio.get_event_loop() @asyncio.coroutine def go(): conn = yield from aioredis.create_connection( ('localhost', 6379), loop=loop) yield from conn.execute('set', 'my-key', 'value') val = yield from conn.execute('get', 'my-key') print(val) conn.close() loop.run_until_complete(go()) # will print 'value'
连接池:
import asyncio import aioredis loop = asyncio.get_event_loop() @asyncio.coroutine def go(): pool = yield from aioredis.create_pool( ('localhost', 6379), minsize=5, maxsize=10, loop=loop) with (yield from pool) as redis: # high-level redis API instance yield from redis.set('my-key', 'value') print((yield from redis.get('my-key'))) pool.clear() # closing all open connections loop.run_until_complete(go()