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() 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):