Skip to content
This repository has been archived by the owner on Dec 10, 2018. It is now read-only.

Ooooooooone way implementation #49

Merged
merged 2 commits into from
Sep 29, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions examples/oneway/sleep.thrift
Original file line number Diff line number Diff line change
@@ -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)
}
19 changes: 19 additions & 0 deletions examples/oneway/sleep_client.py
Original file line number Diff line number Diff line change
@@ -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()
25 changes: 25 additions & 0 deletions examples/oneway/sleep_server.py
Original file line number Diff line number Diff line change
@@ -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()
3 changes: 3 additions & 0 deletions thriftpy/parser/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
14 changes: 11 additions & 3 deletions thriftpy/thrift.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -225,16 +230,19 @@ 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()
except Exception as e:
# 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):
Expand Down