Created
June 27, 2012 08:53
-
-
Save tomotaka/3002565 to your computer and use it in GitHub Desktop.
with_normal_msgpack_rpc_python.py
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
# 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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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?