THRIFT-3454 Python Tornado tutorial is broken
Client: Python
Patch: Nobuaki Sukegawa
This closes #725
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 ioloop
-@gen.engine
-def communicate(callback=None):
+@gen.coroutine
+def communicate():
# create client
transport = TTornado.TTornadoStreamTransport('localhost', 9090)
+ # open the transport, bail on error
+ try:
+ yield transport.open()
+ print('Transport is opened')
+ except TTransport.TTransportException as ex:
+ logging.error(ex)
+ raise gen.Return()
+
pfactory = TBinaryProtocol.TBinaryProtocolFactory()
client = Calculator.Client(transport, pfactory)
- # open the transport, bail on error
- try:
- yield gen.Task(transport.open)
- except TTransport.TTransportException as ex:
- logging.error(ex)
- if callback:
- callback()
- return
-
# 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 @@
# 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 @@
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__":
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 @@
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 @@
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():
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