Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

If the function returns a future, handle things asynchronously #12

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
16 changes: 15 additions & 1 deletion tornadorpc/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,21 @@ def dispatch(self, method_name, params):
self.traceback(method_name, params)
return self.handler.result(self.faults.internal_error())

if getattr(method, 'async', False):
from tornado.concurrent import Future
if isinstance(response, Future):
# self.handler is actually a transient: BaseRPCParser is used as a
# singleton class where self.handler changes each time a request is
# processed, so we need to treat it as a temporary, that may change
# anytime control flow returns to Tornado's IO handler
def make_completer(handler):
def completer(future):
handler.result(future.result())
return completer
if response.done():
return self.handler.result(response.result())
else:
tornado.ioloop.IOLoop.instance().add_future(response, make_completer(self.handler))
elif getattr(method, 'async', False):
# Asynchronous response -- the method should have called
# self.result(RESULT_VALUE)
if response is not None:
Expand Down