Skip to content

Instantly share code, notes, and snippets.

@tab1293
Created February 11, 2019 20:42
Show Gist options
  • Select an option

  • Save tab1293/c5b7d069d637f5d85b7466c5d770aad7 to your computer and use it in GitHub Desktop.

Select an option

Save tab1293/c5b7d069d637f5d85b7466c5d770aad7 to your computer and use it in GitHub Desktop.
Attempt to record GRPC response time
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