Skip to content

Instantly share code, notes, and snippets.

@tomotaka
Created June 27, 2012 08:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tomotaka/3002565 to your computer and use it in GitHub Desktop.
Save tomotaka/3002565 to your computer and use it in GitHub Desktop.
with_normal_msgpack_rpc_python.py
# perform async MessagePack RPC request
# base idea is from: https://github.com/msgpack/msgpack-rpc-python/pull/5
import tornado.web
import tornado.gen
import tornado.httpserver
import tornado.ioloop
import msgpackrpc
import msgpackrpc.loop
class AsyncLoop(msgpackrpc.loop.Loop):
def stop(self):
pass
class MyHandler(tornado.web.RequestHandler):
#@tornado.gen.engine
@tornado.web.asynchronous
def get(self):
try:
x = int(self.get_argument('x'))
y = int(self.get_argument('y'))
except:
self.write("x and y are not given")
else:
print "x=" + str(x) + ", y=" + str(y)
def cb(result):
print "called cb!"
r = result.result
print "result=" + str(r)
self.write("x=" + str(x) + ", y=" + str(y) + ", result=" + str(r))
self.finish()
client = msgpackrpc.Client(msgpackrpc.Address('localhost', 3344), loop=AsyncLoop(tornado.ioloop.IOLoop.instance()))
future = client.call_async("sum", x, y)
future.attach_callback(cb)
if __name__ == '__main__':
app = tornado.web.Application(
[
(r'/', MyHandler)
]
)
server = tornado.httpserver.HTTPServer(app)
server.listen(7788)
print "port=7788"
tornado.ioloop.IOLoop.instance().start()
@huangwei1024
Copy link

good, i just find this solution! i confuse with msgpackrcp's Future get function will switch tornadoweb ioloop. msgpackrpc can't handle the ioloop switch well done?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment