PEP 3156 implementation of the redis protocol.
Project description
Redis client for the PEP 3156 Python event loop.
This Redis library is a completely asynchronous, non-blocking client for a Redis server. It depends on asyncio (PEP 3156) and requires Python 3.6 or greater. If you’re new to asyncio, it can be helpful to check out the asyncio documentation first.
Maintainers needed!
Right now, this library is working fine, but not actively maintained, due to lack of time and shift of priorities on my side (Jonathan). Most of my time doing open source goes to prompt_toolkt community.
I still merge pull request when they are fine, especially for bug/security fixes. But for a while now, we don’t have new features. If you are already using it, then there’s not really a need to worry, asyncio-redis will keep working fine, and we fix bugs, but it’s not really evolving.
If anyone is interested to seriously take over development, please let me know. Also keep in mind that there is a competing library called aioredis, which does have a lot of activity.
See issue https://github.com/jonathanslenders/asyncio-redis/issues/134 to discuss.
Features
Works for the asyncio (PEP3156) event loop
No dependencies except asyncio
Connection pooling
Automatic conversion from unicode (Python) to bytes (inside Redis.)
Bytes and str protocols.
Completely tested
Blocking calls and transactions supported
Streaming of some multi bulk replies
Pubsub support
Trollius support: There is a fork by Ben Jolitz that has the necessary changes for using this asyncio-redis library with Trollius.
Installation
pip install asyncio_redis
Documentation
View documentation at read-the-docs
The connection class
A asyncio_redis.Connection instance will take care of the connection and will automatically reconnect, using a new transport when the connection drops. This connection class also acts as a proxy to a asyncio_redis.RedisProtocol instance; any Redis command of the protocol can be called directly at the connection.
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())
Connection pooling
Requests will automatically be distributed among all connections in a pool. If a connection is blocking because of –for instance– a blocking rpop, another connection will be used for new commands.
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()
Transactions example
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()
It’s recommended to use a large enough poolsize. A connection will be occupied as long as there’s a transaction running in there.
Pubsub example
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 Scripting example
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()
Example using the Protocol class
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())
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file asyncio_redis-0.16.0.tar.gz
.
File metadata
- Download URL: asyncio_redis-0.16.0.tar.gz
- Upload date:
- Size: 31.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.39.0 CPython/3.7.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ff8ce4e7e22a08e2688ae6b397aeac355473e343ce3c68ae3b713494318d848b |
|
MD5 | 651cc64d56cc38f8f927e22604825058 |
|
BLAKE2b-256 | 055e9639218ee3c4a2d9923fb48486721277773a259e428866faa90246016365 |
File details
Details for the file asyncio_redis-0.16.0-py2.py3-none-any.whl
.
File metadata
- Download URL: asyncio_redis-0.16.0-py2.py3-none-any.whl
- Upload date:
- Size: 34.1 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.39.0 CPython/3.7.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4a134fde5ea3628ff0c7118e2424b0f26140a1bd21d5e4632058f1f662773686 |
|
MD5 | 4b716f998fbd330917b2c4d3ceac0703 |
|
BLAKE2b-256 | 12d2a8439844ff26cfc03520f5053c0dfe8b90d869553913c6b94164de357072 |