Contents:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | import tornado.ioloop
import tornado.web
import tornado.gen
from datetime import timedelta
import chu.rpc
@tornado.gen.engine
def simple_rpc():
# Make sure you have rabbitmq running locally, or modify localhost
client = chu.rpc.AsyncTornadoRPCClient(host='localhost')
# The params passed to RPCRequest are serialized to JSON
rpc_request = chu.rpc.RPCRequest(exchange='exch',
routing_key='key',
params=dict(ncc=1701))
# client.rpc returns a future
future = yield tornado.gen.Task(client.rpc, rpc_request)
try:
# We yield the future's get method as a Task to wait for the response
response = yield tornado.gen.Task(future.get,
timeout=timedelta(seconds=6))
except chu.rpc.RPCTimeoutError as e:
print "Oops, we didn't get a response"
# clean up the IOLoop, since this is a singleton example
tornado.ioloop.IOLoop.instance().add_callback(io_loop.stop)
if __name__ == "__main__":
io_loop = tornado.ioloop.IOLoop.instance()
io_loop.add_callback(simple_rpc)
io_loop.add_timeout(timedelta(seconds=10), io_loop.stop)
io_loop.start()
|