THRIFT-3454 Python Tornado tutorial is broken Client: Python Patch: Nobuaki Sukegawa
This closes #725 Project: http://git-wip-us.apache.org/repos/asf/thrift/repo Commit: http://git-wip-us.apache.org/repos/asf/thrift/commit/2e00c999 Tree: http://git-wip-us.apache.org/repos/asf/thrift/tree/2e00c999 Diff: http://git-wip-us.apache.org/repos/asf/thrift/diff/2e00c999 Branch: refs/heads/master Commit: 2e00c9998f1aa316c8d0168488887fb957845230 Parents: 70e6c29 Author: Nobuaki Sukegawa <[email protected]> Authored: Tue Dec 1 23:46:58 2015 +0900 Committer: Nobuaki Sukegawa <[email protected]> Committed: Fri Dec 4 00:38:33 2015 +0900 ---------------------------------------------------------------------- tutorial/py.tornado/PythonClient.py | 56 ++++++++++++++------------------ tutorial/py.tornado/PythonServer.py | 21 ++++++------ tutorial/tutorial.thrift | 2 +- 3 files changed, 35 insertions(+), 44 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/thrift/blob/2e00c999/tutorial/py.tornado/PythonClient.py ---------------------------------------------------------------------- diff --git a/tutorial/py.tornado/PythonClient.py b/tutorial/py.tornado/PythonClient.py index 005ad3c..426146f 100755 --- a/tutorial/py.tornado/PythonClient.py +++ b/tutorial/py.tornado/PythonClient.py @@ -19,18 +19,17 @@ # under the License. # -import sys import glob +import logging +import sys + sys.path.append('gen-py.tornado') sys.path.insert(0, glob.glob('../../lib/py/build/lib*')[0]) -import logging - from tutorial import Calculator from tutorial.ttypes import Operation, Work, InvalidOperation from thrift import TTornado -from thrift.transport import TSocket from thrift.transport import TTransport from thrift.protocol import TBinaryProtocol @@ -38,29 +37,28 @@ from tornado import gen from tornado import ioloop [email protected] -def communicate(callback=None): [email protected] +def communicate(): # create client transport = TTornado.TTornadoStreamTransport('localhost', 9090) - pfactory = TBinaryProtocol.TBinaryProtocolFactory() - client = Calculator.Client(transport, pfactory) - # open the transport, bail on error try: - yield gen.Task(transport.open) + yield transport.open() + print('Transport is opened') except TTransport.TTransportException as ex: logging.error(ex) - if callback: - callback() - return + raise gen.Return() + + pfactory = TBinaryProtocol.TBinaryProtocolFactory() + client = Calculator.Client(transport, pfactory) # ping - yield gen.Task(client.ping) + yield client.ping() print("ping()") # add - sum_ = yield gen.Task(client.add, 1, 1) - print("1 + 1 = {}".format(sum_)) + sum_ = yield client.add(1, 1) + print("1 + 1 = {0}".format(sum_)) # make a oneway call without a callback (schedule the write and continue # without blocking) @@ -69,7 +67,7 @@ def communicate(callback=None): # make a oneway call with a callback (we'll wait for the stream write to # complete before continuing) - yield gen.Task(client.zip) + client.zip() print("zip() with callback") # calculate 1/0 @@ -79,37 +77,31 @@ def communicate(callback=None): work.num2 = 0 try: - quotient = yield gen.Task(client.calculate, 1, work) - print("Whoa? You know how to divide by zero?") + quotient = yield client.calculate(1, work) + print("Whoa? You know how to divide by zero ? -> {0}".format(quotient)) except InvalidOperation as io: - print("InvalidOperation: {}".format(io)) + print("InvalidOperation: {0}".format(io)) # calculate 15-10 work.op = Operation.SUBTRACT work.num1 = 15 work.num2 = 10 - diff = yield gen.Task(client.calculate, 1, work) - print("15 - 10 = {}".format(diff)) + diff = yield client.calculate(1, work) + print("15 - 10 = {0}".format(diff)) # getStruct - log = yield gen.Task(client.getStruct, 1) - print("Check log: {}".format(log.value)) + log = yield client.getStruct(1) + print("Check log: {0}".format(log.value)) # close the transport client._transport.close() - - if callback: - callback() + raise gen.Return() def main(): # create an ioloop, do the above, then stop - io_loop = ioloop.IOLoop.instance() - def this_joint(): - communicate(callback=io_loop.stop) - io_loop.add_callback(this_joint) - io_loop.start() + ioloop.IOLoop.current().run_sync(communicate) if __name__ == "__main__": http://git-wip-us.apache.org/repos/asf/thrift/blob/2e00c999/tutorial/py.tornado/PythonServer.py ---------------------------------------------------------------------- diff --git a/tutorial/py.tornado/PythonServer.py b/tutorial/py.tornado/PythonServer.py index 4198214..7b750e4 100755 --- a/tutorial/py.tornado/PythonServer.py +++ b/tutorial/py.tornado/PythonServer.py @@ -19,8 +19,9 @@ # under the License. # -import sys import glob +import sys + sys.path.append('gen-py.tornado') sys.path.insert(0, glob.glob('../../lib/py/build/lib*')[0]) @@ -42,15 +43,14 @@ class CalculatorHandler(object): def __init__(self): self.log = {} - def ping(self, callback): + def ping(self): print("ping()") - callback() - def add(self, n1, n2, callback): + def add(self, n1, n2): print("add({}, {})".format(n1, n2)) - callback(n1 + n2) + return n1 + n2 - def calculate(self, logid, work, callback): + def calculate(self, logid, work): print("calculate({}, {})".format(logid, work)) if work.op == Operation.ADD: @@ -76,15 +76,14 @@ class CalculatorHandler(object): log.key = logid log.value = '%d' % (val) self.log[logid] = log - callback(val) + return val - def getStruct(self, key, callback): + def getStruct(self, key): print("getStruct({})".format(key)) - callback(self.log[key]) + return self.log[key] - def zip(self, callback): + def zip(self): print("zip()") - callback() def main(): http://git-wip-us.apache.org/repos/asf/thrift/blob/2e00c999/tutorial/tutorial.thrift ---------------------------------------------------------------------- diff --git a/tutorial/tutorial.thrift b/tutorial/tutorial.thrift index 1149edf..c4a96f0 100644 --- a/tutorial/tutorial.thrift +++ b/tutorial/tutorial.thrift @@ -32,7 +32,7 @@ * The first thing to know about are types. The available types in Thrift are: * * bool Boolean, one byte - * byte Signed byte + * i8 (byte) Signed 8-bit integer * i16 Signed 16-bit integer * i32 Signed 32-bit integer * i64 Signed 64-bit integer
