Package: asyncio-redis
Redis client for thePEP 3156Python 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 outthe asyncio documentationfirst.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 calledaioredis, which
does have a lot of activity.See issuehttps://github.com/jonathanslenders/asyncio-redis/issues/134to
discuss.FeaturesWorks for the asyncio (PEP3156) event loopNo dependencies except asyncioConnection poolingAutomatic conversion from unicode (Python) to bytes (inside Redis.)Bytes and str protocols.Completely testedBlocking calls and transactions supportedStreaming of some multi bulk repliesPubsub supportTrollius support: There isa fork by Ben Jolitzthat has the necessary
changes for using this asyncio-redis library with Trollius.Installationpip install asyncio_redisDocumentationView documentation atread-the-docsThe connection classAasyncio_redis.Connectioninstance 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 aasyncio_redis.RedisProtocolinstance; any Redis command of the protocol can be called directly at the
connection.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())Connection poolingRequests 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.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()Transactions exampleimportasyncioimportasyncio_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()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 exampleimportasyncioimportasyncio_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 Scripting exampleimportasyncioimportasyncio_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()Example using the Protocol classimportasyncioimportasyncio_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())