Package: trollius_redis
(ported fromasyncio-redis)SupportsCPython 2.7, 3.3-3.5PyPyPyPy3DescriptionRedis client for thePEP 3156Python event loop ported to Trollius.This Redis library is a completely asynchronous, non-blocking client for a
Redis server. It depends on trollius (asyncio compatible for PEP 3156). It
supports Python 2 and 3 Trollius-using developers.If you’re new to asyncio, it can be helpful to check outthe asyncio documentationfirst.To see the original awesome driver that I ported from, I advise you to take a look at Jonathan Slendersasyncio-redis.FeaturesWorks for the trollius asyncio-compatible (PEP3156) event loopNo dependencies except trolliusConnection poolingAutomatic conversion from unicode (Python) to bytes (inside Redis.)Bytes and str protocols.Completely testedBlocking calls and transactions supportedStreaming of some multi bulk repliesPubsub supportInstallationpip install trollius-redisDocumentationView documentation atread-the-docsThe connection classAtrollius_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 atrollius_redis.RedisProtocolinstance; any Redis command of the protocol can be called directly at the
connection.importtrolliusfromtrolliusimportFromimporttrollius_redis@trollius.coroutinedefexample():# Create Redis connectionconnection=yieldFrom(trollius_redis.Connection.create(host=u'localhost',port=6379))# Set a keyyieldFrom(connection.set(u'my_key',u'my_value'))# When finished, close the connection.connection.close()if__name__=='__main__':loop=trollius.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.importtrolliusfromtrolliusimportFromimporttrollius_redis@trollius.coroutinedefexample():# Create Redis connectionconnection=yieldFrom(trollius_redis.Pool.create(host=u'localhost',port=6379,poolsize=10))# Set a keyyieldFrom(connection.set(u'my_key',u'my_value'))# When finished, close the connection pool.connection.close()Transactions exampleimporttrolliusfromtrolliusimportFromimporttrollius_redis@trollius.coroutinedefexample():# Create Redis connectionconnection=yieldFrom(trollius_redis.Pool.create(host=u'localhost',port=6379,poolsize=10))# Create transactiontransaction=yieldFrom(connection.multi())# Run commands in transaction (they return future objects)f1=yieldFrom(transaction.set(u'key',u'value'))f2=yieldFrom(transaction.set(u'another_key',u'another_value'))# Commit transactionyieldFrom(transaction.execute())# Retrieve resultsresult1=yieldFrom(f1)result2=yieldFrom(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 exampleimporttrolliusfromtrolliusimportFromimporttrollius_redis@trollius.coroutinedefexample():# Create connectionconnection=yieldFrom(trollius_redis.Connection.create(host=u'localhost',port=6379))# Create subscriber.subscriber=yieldFrom(connection.start_subscribe())# Subscribe to channel.yieldFrom(subscriber.subscribe([u'our-channel']))# Inside a while loop, wait for incoming events.whileTrue:reply=yieldFrom(subscriber.next_published())print(u'Received: ',repr(reply.value),u'on channel',reply.channel)# When finished, close the connection.connection.close()LUA Scripting exampleimporttrolliusfromtrolliusimportFromimporttrollius_rediscode=\u"""
local value = redis.call('GET', KEYS[1])
value = tonumber(value)
return value * ARGV[1]
"""@trollius.coroutinedefexample():connection=yieldFrom(trollius_redis.Connection.create(host=u'localhost',port=6379))# Set a keyyieldFrom(connection.set(u'my_key',u'2'))# Register scriptmultiply=yieldFrom(connection.register_script(code))# Run scriptscript_reply=yieldFrom(multiply.run(keys=[u'my_key'],args=[u'5']))result=yieldFrom(script_reply.return_value())print(result)# prints 2 * 5# When finished, close the connection.connection.close()Example using the Protocol classimporttrolliusfromtrolliusimportFromimporttrollius_redis@trollius.coroutinedefexample():loop=trollius.get_event_loop()# Create Redis connectiontransport,protocol=yieldFrom(loop.create_connection(trollius_redis.RedisProtocol,u'localhost',6379))# Set a keyyieldFrom(protocol.set(u'my_key',u'my_value'))# Get a keyresult=yieldFrom(protocol.get(u'my_key'))print(result)# Close transport when finished.transport.close()if__name__=='__main__':trollius.get_event_loop().run_until_complete(example())