From b6cf624c8d90dc6eaccb3b8c36ea82b71649b52c Mon Sep 17 00:00:00 2001 From: hit9 Date: Mon, 29 Sep 2014 15:02:42 +0800 Subject: [PATCH 1/2] oneway implementation --- thriftpy/parser/__init__.py | 3 +++ thriftpy/thrift.py | 14 +++++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/thriftpy/parser/__init__.py b/thriftpy/parser/__init__.py index b981fd9..50955f0 100644 --- a/thriftpy/parser/__init__.py +++ b/thriftpy/parser/__init__.py @@ -190,6 +190,9 @@ def _ttype_spec(ttype, name): result_name = "%s_result" % api_name result_attrs = {"__module__": module_name} + # if oneway + result_attrs["oneway"] = api["oneway"] + if api["type"] == "void": result_thrift_spec = {} else: diff --git a/thriftpy/thrift.py b/thriftpy/thrift.py index 24b4c31..135e4f0 100644 --- a/thriftpy/thrift.py +++ b/thriftpy/thrift.py @@ -133,7 +133,12 @@ def _req(self, api, *args, **kwargs): *args) kwargs.update(_kw) self._send(api, **kwargs) - return self._recv(api) + + result_cls = getattr(self._service, api + "_result") + + if not getattr(result_cls, "oneway"): + # wait result only if non-oneway + return self._recv(api) def _send(self, api, **kwargs): self._oprot.write_message_begin(api, TMessageType.CALL, self._seqid) @@ -225,8 +230,10 @@ def handle_exception(self, e, result): def process(self, iprot, oprot): api, seqid, result, call = self.process_in(iprot) + if isinstance(result, TApplicationException): - self.send_exception(oprot, api, result, seqid) + if not result.oneway: + self.send_exception(oprot, api, result, seqid) else: try: result.success = call() @@ -234,7 +241,8 @@ def process(self, iprot, oprot): # raise if api don't have throws self.handle_exception(e, result) - self.send_result(oprot, api, result, seqid) + if not result.oneway: + self.send_result(oprot, api, result, seqid) class TException(TPayload, Exception): From 4fb6744eb617a78935562cfbb90bdf84007cfcc6 Mon Sep 17 00:00:00 2001 From: hit9 Date: Mon, 29 Sep 2014 15:15:30 +0800 Subject: [PATCH 2/2] add oneway example: sleep --- examples/oneway/sleep.thrift | 9 +++++++++ examples/oneway/sleep_client.py | 19 +++++++++++++++++++ examples/oneway/sleep_server.py | 25 +++++++++++++++++++++++++ 3 files changed, 53 insertions(+) create mode 100644 examples/oneway/sleep.thrift create mode 100644 examples/oneway/sleep_client.py create mode 100644 examples/oneway/sleep_server.py diff --git a/examples/oneway/sleep.thrift b/examples/oneway/sleep.thrift new file mode 100644 index 0000000..917e57c --- /dev/null +++ b/examples/oneway/sleep.thrift @@ -0,0 +1,9 @@ +/** + * Simple example to show how to use `oneway` with thriftpy. + */ +service Sleep { + /** + * Tell the server to sleep! + */ + oneway void sleep(1: i32 seconds) +} diff --git a/examples/oneway/sleep_client.py b/examples/oneway/sleep_client.py new file mode 100644 index 0000000..6354115 --- /dev/null +++ b/examples/oneway/sleep_client.py @@ -0,0 +1,19 @@ +# -*- coding: utf-8 -*- + +import thriftpy +sleep_thrift = thriftpy.load("sleep.thrift", module_name="sleep_thrift") + +from thriftpy.rpc import make_client + + +def main(): + client = make_client(sleep_thrift.Sleep, '127.0.0.1', 6000) + # sleep multiple times, but the client won't wait any more + client.sleep(1) + client.sleep(2) + client.sleep(3) + client.sleep(4) + + +if __name__ == '__main__': + main() diff --git a/examples/oneway/sleep_server.py b/examples/oneway/sleep_server.py new file mode 100644 index 0000000..a65ff39 --- /dev/null +++ b/examples/oneway/sleep_server.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- + +import time +import thriftpy +sleep_thrift = thriftpy.load("sleep.thrift", module_name="sleep_thrift") + +from thriftpy.rpc import make_server + + +class Dispatcher(object): + def sleep(self, seconds): + print("I'm going to sleep %d seconds" % seconds) + time.sleep(seconds) + print("Sleep over!") + + +def main(): + server = make_server(sleep_thrift.Sleep, Dispatcher(), + '127.0.0.1', 6000) + print("serving...") + server.serve() + + +if __name__ == '__main__': + main()