跳转到主要内容

PEP 3156协议的实现。

项目描述

Build Status

PEP 3156 Python事件循环的Redis客户端。

这个Redis库是一个完全异步、非阻塞的Redis服务器客户端。它依赖于asyncio (PEP 3156)并且需要Python 3.6或更高版本。如果你是asyncio的新手,可以先查看asyncio文档

需要维护者!

目前,这个库运行良好,但不是积极维护的,因为时间和优先级的原因(Jonathan)。我大部分开源时间花在prompt_toolkt社区。

我仍然会合并好的pull请求,尤其是针对错误/安全修复。但有一段时间没有新功能了。如果你已经在使用它,那么其实没有必要担心,asyncio-redis仍然会正常工作,我们会修复错误,但它并没有真正进化。

如果有人有兴趣认真接管开发,请让我知道。同时请注意,还有一个名为aioredis的竞争库,它有很多活动。

查看问题https://github.com/jonathanslenders/asyncio-redis/issues/134进行讨论。

功能

  • 适用于asyncio (PEP3156)事件循环

  • 除了asyncio外没有依赖项

  • 连接池

  • 自动将unicode(Python)转换为bytes(Redis内部)

  • bytes和str协议

  • 完全测试

  • 支持阻塞调用和事务

  • 一些多批量回复的流式传输

  • 支持pubsub

Trollius支持:有一个由Ben Jolitz的分支,它对使用此asyncio-redis库进行了必要的更改。

安装

pip install asyncio_redis

文档

read-the-docs 查看文档

连接类

一个 asyncio_redis.Connection 实例将负责连接,并在连接断开时自动重新连接,使用新的传输。此连接类还充当 asyncio_redis.RedisProtocol 实例的代理;可以直接在连接处调用任何 Redis 协议命令。

import asyncio
import asyncio_redis

@asyncio.coroutine
def example():
    # Create Redis connection
    connection = yield from asyncio_redis.Connection.create(host='127.0.0.1', port=6379)

    # Set a key
    yield from connection.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,连接被阻塞,则会使用另一个连接来执行新的命令。

import asyncio
import asyncio_redis

@asyncio.coroutine
def example():
    # Create Redis connection
    connection = yield from asyncio_redis.Pool.create(host='127.0.0.1', port=6379, poolsize=10)

    # Set a key
    yield from connection.set('my_key', 'my_value')

    # When finished, close the connection pool.
    connection.close()

事务示例

import asyncio
import asyncio_redis

@asyncio.coroutine
def example():
    # Create Redis connection
    connection = yield from asyncio_redis.Pool.create(host='127.0.0.1', port=6379, poolsize=10)

    # Create transaction
    transaction = yield from connection.multi()

    # Run commands in transaction (they return future objects)
    f1 = yield from transaction.set('key', 'value')
    f2 = yield from transaction.set('another_key', 'another_value')

    # Commit transaction
    yield from transaction.exec()

    # Retrieve results
    result1 = yield from f1
    result2 = yield from f2

    # When finished, close the connection pool.
    connection.close()

建议使用足够大的 poolsize。只要其中运行着事务,连接就会被占用。

发布/订阅示例

import asyncio
import asyncio_redis

@asyncio.coroutine
def example():
    # Create connection
    connection = yield from asyncio_redis.Connection.create(host='127.0.0.1', port=6379)

    # Create subscriber.
    subscriber = yield from connection.start_subscribe()

    # Subscribe to channel.
    yield from subscriber.subscribe([ 'our-channel' ])

    # Inside a while loop, wait for incoming events.
    while True:
        reply = yield from subscriber.next_published()
        print('Received: ', repr(reply.value), 'on channel', reply.channel)

    # When finished, close the connection.
    connection.close()

LUA 脚本示例

import asyncio
import asyncio_redis

code = \
"""
local value = redis.call('GET', KEYS[1])
value = tonumber(value)
return value * ARGV[1]
"""

@asyncio.coroutine
def example():
    connection = yield from asyncio_redis.Connection.create(host='127.0.0.1', port=6379)

    # Set a key
    yield from connection.set('my_key', '2')

    # Register script
    multiply = yield from connection.register_script(code)

    # Run script
    script_reply = yield from multiply.run(keys=['my_key'], args=['5'])
    result = yield from script_reply.return_value()
    print(result) # prints 2 * 5

    # When finished, close the connection.
    connection.close()

使用 Protocol 类的示例

import asyncio
import asyncio_redis

@asyncio.coroutine
def example():
    loop = asyncio.get_event_loop()

    # Create Redis connection
    transport, protocol = yield from loop.create_connection(
                asyncio_redis.RedisProtocol, '127.0.0.1', 6379)

    # Set a key
    yield from protocol.set('my_key', 'my_value')

    # Get a key
    result = yield from protocol.get('my_key')
    print(result)

    # Close transport when finished.
    transport.close()

if __name__ == '__main__':
    asyncio.get_event_loop().run_until_complete(example())

项目详情


下载文件

下载适用于您的平台的文件。如果您不确定要选择哪个,请了解有关 安装包 的更多信息。

源分布

asyncio_redis-0.16.0.tar.gz (31.7 kB 查看哈希)

上传时间

构建分布

asyncio_redis-0.16.0-py2.py3-none-any.whl (34.1 kB 查看哈希)

上传时间 Python 2 Python 3

支持者

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面