Created
February 11, 2019 20:42
-
-
Save tab1293/c5b7d069d637f5d85b7466c5d770aad7 to your computer and use it in GitHub Desktop.
Attempt to record GRPC response time
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from tornado.ioloop import IOLoop | |
| from tornado.gen import Future | |
| def _fwrap(f, gf, ioloop, start_time): | |
| try: | |
| gf_result = gf.result() | |
| response_time = ioloop.time() - start_time | |
| f.set_result(gf_result) | |
| except Exception as e: | |
| f.set_exception(e) | |
| def fwrap(gf, ioloop=None): | |
| ''' | |
| Wraps a GRPC result in a future that can be yielded by tornado | |
| Usage:: | |
| @coroutine | |
| def my_fn(param): | |
| result = yield fwrap(stub.function_name.future(param, timeout)) | |
| ''' | |
| f = Future() | |
| if ioloop is None: | |
| ioloop = IOLoop.current() | |
| start_time = ioloop.time() | |
| gf.add_done_callback(lambda _: ioloop.add_callback(_fwrap, f, gf, ioloop, start_time)) | |
| return f |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment