Are you happy with your logging solution? Would you help us out by taking a 30-second survey? Click here

asyncio-redis

Redis client for Python asyncio (PEP 3156)

Subscribe to updates I use asyncio-redis


Statistics on asyncio-redis

Number of watchers on Github 409
Number of open issues 42
Average time to close an issue about 1 month
Main language Python
Average time to merge a PR 5 days
Open pull requests 9+
Closed pull requests 13+
Last commit over 2 years ago
Repo Created about 6 years ago
Repo Last Updated over 1 year ago
Size 462 KB
Homepage http://asyncio-re...
Organization / Authorjonathanslenders
Contributors17
Page Updated
Do you use asyncio-redis? Leave a review!
View open issues (42)
View asyncio-redis activity
View on github
Fresh, new opensource launches 🚀🚀🚀
Trendy new open source projects in your inbox! View examples

Subscribe to our mailing list

Evaluating asyncio-redis for your project? Score Explanation
Commits Score (?)
Issues & PR Score (?)

Redis client for Python asyncio.

|Build Status|

.. # Don't show the Build status on drone.io. Update script first. It's still # using an older Redis version. # |Build Status2|

Redis client for the PEP 3156_ Python event loop.

.. _PEP 3156: http://legacy.python.org/dev/peps/pep-3156/

This Redis library is a completely asynchronous, non-blocking client for a Redis server. It depends on asyncio (PEP 3156) and therefor it requires Python 3.3 or greater. If you're new to asyncio, it can be helpful to check out the asyncio documentation_ first.

.. _the asyncio documentation: http://docs.python.org/dev/library/asyncio.html

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.

.. _a fork by Ben Jolitz: https://github.com/benjolitz/trollius-redis

Installation

.. code::

pip install asyncio_redis

Documentation

View documentation at read-the-docs_

.. _read-the-docs: http://asyncio-redis.readthedocs.org/en/latest/

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.

.. code:: python

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.

.. code:: python

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

.. code:: python

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

.. code:: python

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

.. code:: python

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

.. code:: python

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())

.. |Build Status| image:: https://travis-ci.org/jonathanslenders/asyncio-redis.png :target: https://travis-ci.org/jonathanslenders/asyncio-redis#

.. |Build Status2| image:: https://drone.io/github.com/jonathanslenders/asyncio-redis/status.png :target: https://drone.io/github.com/jonathanslenders/asyncio-redis/latest

asyncio-redis open issues Ask a question     (View All Issues)
  • about 3 years subscribing to an empty set kills pubsub
  • about 3 years Cleanup pubsub?
  • over 3 years Sentinel support
  • over 3 years Raise UnicodeEncodeError when register a script that contain non-ascii chars.
  • over 3 years HiRedisProtocol's subscription service is broken
  • almost 4 years Consider `async for` implementation for pubsub API's
  • almost 4 years Is this project now support?
  • almost 4 years AssertionError in multibulk_as_scanpart
  • about 4 years pip jessie install report
  • about 4 years Bad handling of timeouts in pubsub subscriptions
  • about 4 years pubsub incompatible with auto_reconnect
  • about 4 years Problems with setup.py
  • about 4 years Ability to fetch specific connection from Pool
  • over 4 years Add support for redis' new Geo commands
  • over 4 years Is it possible to return an Error when attempting to connect to non-running Redis Server?
  • over 4 years Python 2 Trollius support
  • over 4 years pool reallocates non-free connection whenever use multi method
  • over 4 years Redis Cluster Support
  • over 4 years z* commands overly pedantic about types
  • almost 5 years Pending task destroyed warning
  • over 5 years Support new features from the 2.8.9 release
  • over 5 years Any way to ping Subscription connection
  • over 5 years File descriptor leak when can't connect to host
  • over 5 years Doc improvements.
asyncio-redis open pull requests (View All Pulls)
  • fix warning for deprecated async()
  • added redis geo commands
  • Support optional blocking for close
  • Add an option to avoid blocking in `{Connection,Pool}.create`
  • Sentinel support
  • added options for zadd and zincrby
  • Adding parser for InfoReply and ClientListReply
  • Fix docs in example Pool usage
  • "Auto_reconnect" not work for first conntion.
asyncio-redis list of languages used
Other projects in Python