Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
57 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,34 @@ | ||
# Let's import tornado and tornadis | ||
import tornado | ||
import tornadis | ||
import logging | ||
logging.basicConfig(level=logging.CRITICAL) | ||
|
||
|
||
def ping_callback(result): | ||
if not isinstance(result, tornadis.TornadisException): | ||
print result | ||
# For specific reasons, tornadis nearly never raises any exception | ||
# they are returned as result | ||
print "got exception: %s" % result | ||
else: | ||
# result is already a python object (a string in this simple example) | ||
print "Result: %s" % result | ||
|
||
|
||
@tornado.gen.coroutine | ||
def main(): | ||
# let's (re)connect (autoconnect mode), call the ping redis command | ||
# and wait the reply without blocking the tornado ioloop | ||
# Note: async_call() method on Client instance does not return anything | ||
# but the callback will be called later with the result. | ||
client.async_call("PING", callback=ping_callback) | ||
yield tornado.gen.sleep(1) | ||
|
||
|
||
# Build a tornadis.Client object with some options as kwargs | ||
# host: redis host to connect | ||
# port: redis port to connect | ||
# autoconnect=True: put the Client object in auto(re)connect mode | ||
client = tornadis.Client(host="localhost", port=6379, autoconnect=True) | ||
|
||
# Start a tornado IOLoop, execute the coroutine and end the program | ||
loop = tornado.ioloop.IOLoop.instance() | ||
client = tornadis.Client() | ||
loop.run_sync(main) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,37 @@ | ||
# Let's import tornado and tornadis | ||
import tornado | ||
import tornadis | ||
|
||
|
||
@tornado.gen.coroutine | ||
def pipeline_coroutine(): | ||
# Let's get a connected client | ||
client = tornadis.Client() | ||
|
||
# Let's make a pipeline object to stack commands inside | ||
pipeline = tornadis.Pipeline() | ||
pipeline.stack_call("SET", "foo", "bar") | ||
pipeline.stack_call("GET", "foo") | ||
|
||
# At this point, nothing is sent to redis | ||
|
||
# Let's submit the pipeline to redis and wait for replies | ||
# let's (re)connect (autoconnect mode), send the pipeline of requests | ||
# (atomic mode) and wait all replies without blocking the tornado ioloop. | ||
results = yield client.call(pipeline) | ||
|
||
# The two replies are in the results array | ||
print results | ||
# >>> ['OK', 'bar'] | ||
if isinstance(results, tornadis.TornadisException): | ||
# For specific reasons, tornadis nearly never raises any exception | ||
# they are returned as result | ||
print "got exception: %s" % result | ||
else: | ||
# The two replies are in the results array | ||
print results | ||
# >>> ['OK', 'bar'] | ||
|
||
# Let's disconnect | ||
client.disconnect() | ||
|
||
# Build a tornadis.Client object with some options as kwargs | ||
# host: redis host to connect | ||
# port: redis port to connect | ||
# autoconnect=True: put the Client object in auto(re)connect mode | ||
client = tornadis.Client(host="localhost", port=6379, autoconnect=True) | ||
|
||
# Start a tornado IOLoop, execute the coroutine and end the program | ||
loop = tornado.ioloop.IOLoop.instance() | ||
loop.run_sync(pipeline_coroutine) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters