PEP 3156 Implementation of the Redis Protocol.
asyncio_redis的Python项目详细描述
用于PEP 3156python事件循环的redis客户端。
这个redis库是 redis服务器。它依赖于异步(pep 3156),因此需要python 3.3或更高。如果您是Asyncio新手,那么签出 the asyncio documentation先。
功能
- 适用于asyncio(pep3156)事件循环
- 除了asyncio之外没有依赖项
- 连接池
- 从Unicode(Python)到字节(在Redis中)的自动转换。
- 字节和str协议。
- 完全测试
- 支持阻止呼叫和事务
- 一些多批量回复的流媒体
- PubSub支持
trollius支持:有a fork by Ben Jolitz具有必需的 对trollius使用此asyncio redis库所做的更改。
安装
pip install asyncio_redis
文档
在read-the-docs查看文档
连接类
一个asyncio_redis.Connection实例将负责连接和 将自动重新连接,在连接断开时使用新传输。 这个连接类还充当^{tt2}的代理$ 实例;协议的任何redis命令都可以在 连接。
importasyncioimportasyncio_redis@asyncio.coroutinedefexample():# Create Redis connectionconnection=yield fromasyncio_redis.Connection.create(host='127.0.0.1',port=6379)# Set a keyyield fromconnection.set('my_key','my_value')# When finished, close the connection.connection.close()if__name__=='__main__':loop=asyncio.get_event_loop()loop.run_until_complete(example())
连接池
请求将自动分布在池中的所有连接之间。如果 连接被阻塞是因为-例如-阻塞的rpop,另一个 连接将用于新命令。
importasyncioimportasyncio_redis@asyncio.coroutinedefexample():# Create Redis connectionconnection=yield fromasyncio_redis.Pool.create(host='127.0.0.1',port=6379,poolsize=10)# Set a keyyield fromconnection.set('my_key','my_value')# When finished, close the connection pool.connection.close()
事务处理示例
importasyncioimportasyncio_redis@asyncio.coroutinedefexample():# Create Redis connectionconnection=yield fromasyncio_redis.Pool.create(host='127.0.0.1',port=6379,poolsize=10)# Create transactiontransaction=yield fromconnection.multi()# Run commands in transaction (they return future objects)f1=yield fromtransaction.set('key','value')f2=yield fromtransaction.set('another_key','another_value')# Commit transactionyield fromtransaction.exec()# Retrieve resultsresult1=yield fromf1result2=yield fromf2# When finished, close the connection pool.connection.close()
建议使用足够大的池大小。连接将被占用 只要有交易在里面进行。
pubsub示例
importasyncioimportasyncio_redis@asyncio.coroutinedefexample():# Create connectionconnection=yield fromasyncio_redis.Connection.create(host='127.0.0.1',port=6379)# Create subscriber.subscriber=yield fromconnection.start_subscribe()# Subscribe to channel.yield fromsubscriber.subscribe(['our-channel'])# Inside a while loop, wait for incoming events.whileTrue:reply=yield fromsubscriber.next_published()print('Received: ',repr(reply.value),'on channel',reply.channel)# When finished, close the connection.connection.close()
lua脚本示例
importasyncioimportasyncio_rediscode= \ """ local value = redis.call('GET', KEYS[1]) value = tonumber(value) return value * ARGV[1] """@asyncio.coroutinedefexample():connection=yield fromasyncio_redis.Connection.create(host='127.0.0.1',port=6379)# Set a keyyield fromconnection.set('my_key','2')# Register scriptmultiply=yield fromconnection.register_script(code)# Run scriptscript_reply=yield frommultiply.run(keys=['my_key'],args=['5'])result=yield fromscript_reply.return_value()print(result)# prints 2 * 5# When finished, close the connection.connection.close()
使用协议类的示例
importasyncioimportasyncio_redis@asyncio.coroutinedefexample():loop=asyncio.get_event_loop()# Create Redis connectiontransport,protocol=yield fromloop.create_connection(asyncio_redis.RedisProtocol,'127.0.0.1',6379)# Set a keyyield fromprotocol.set('my_key','my_value')# Get a keyresult=yield fromprotocol.get('my_key')print(result)# Close transport when finished.transport.close()if__name__=='__main__':asyncio.get_event_loop().run_until_complete(example())