Skip to main content

PEP 3156 implementation of the redis protocol.

Project description

Build Status

Features

  • Works for the asyncio (PEP3156) event loop

  • No dependencies

  • 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

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='localhost', port=6379)

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

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='localhost', port=6379, poolsize=10)

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

Transactions example

import asyncio
import asyncio_redis

@asyncio.coroutine
def example():
    # Create Redis connection
    connection = yield from asyncio_redis.Pool.create(host='localhost', 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')
    f1 = yield from transaction.set('another_key', 'another_value')

    # Commit transaction
    yield from transaction.exec()

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

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='localhost', 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)

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='localhost', 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

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, 'localhost', 6379)

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

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

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

asyncio_redis-0.9.tar.gz (24.4 kB view details)

Uploaded Source

File details

Details for the file asyncio_redis-0.9.tar.gz.

File metadata

  • Download URL: asyncio_redis-0.9.tar.gz
  • Upload date:
  • Size: 24.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for asyncio_redis-0.9.tar.gz
Algorithm Hash digest
SHA256 a5f2b828ee1f58a2a2c6e7fd5cc23a8666b7d1d18606860a84bd3d19e1773210
MD5 c63f14aeefcc618b2d2a63a22d3a812d
BLAKE2b-256 f34483eeeb7346e892c236222d7da8bc7e80b46bb24faa83e3a9c7042ea3303c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page