def run_sync(self, func: typing.Callable) -> typing.Any:
"""
Run a the given async function on the ioloop of the agent. It will block the current thread until the future
resolves.
:param func: A function that returns a yieldable future.
:return: The result of the async function.
"""
f = Future()
def future_to_future(future):
exc = future.exception()
if exc is not None:
f.set_exception(exc)
else:
f.set_result(future.result())
def run():
try:
result = func()
if result is not None:
from tornado.gen import convert_yielded
result = convert_yielded(result)
result.add_done_callback(future_to_future)
except Exception as e:
f.set_exception(e)
self._ioloop.add_callback(run)
return f.result()
评论列表
文章目录