我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用tornado.gen.convert_yielded()。
def _write_body(self, start_read): if self.request.body is not None: self.connection.write(self.request.body) elif self.request.body_producer is not None: fut = self.request.body_producer(self.connection.write) if fut is not None: fut = gen.convert_yielded(fut) def on_body_written(fut): fut.result() self.connection.finish() if start_read: self._read_response() self.io_loop.add_future(fut, on_body_written) return self.connection.finish() if start_read: self._read_response()
def _run_callback(self, callback): """Runs a callback with error handling. For use in subclasses. """ try: ret = callback() if ret is not None: from tornado import gen # Functions that return Futures typically swallow all # exceptions and store them in the Future. If a Future # makes it out to the IOLoop, ensure its exception (if any) # gets logged too. try: ret = gen.convert_yielded(ret) except gen.BadYieldError: # It's not unusual for add_callback to be used with # methods returning a non-None and non-yieldable # result, which should just be ignored. pass else: self.add_future(ret, lambda f: f.result()) except Exception: self.handle_callback_exception(callback)
def _run_callback(self, callback): """Runs a callback with error handling. For use in subclasses. """ try: ret = callback() if ret is not None: from tornado import gen # Functions that return Futures typically swallow all # exceptions and store them in the Future. If a Future # makes it out to the IOLoop, ensure its exception (if any) # gets logged too. try: ret = gen.convert_yielded(ret) except gen.BadYieldError: # It's not unusual for add_callback to be used with # methods returning a non-None and non-yieldable # result, which should just be ignored. pass else: self.add_future(ret, self._discard_future_result) except Exception: self.handle_callback_exception(callback)
def test_success(self): @inlineCallbacks def fn(): if False: # inlineCallbacks doesn't work with regular functions; # must have a yield even if it's unreachable. yield returnValue(42) f = gen.convert_yielded(fn()) self.assertEqual(f.result(), 42)
def test_failure(self): @inlineCallbacks def fn(): if False: yield 1 / 0 f = gen.convert_yielded(fn()) with self.assertRaises(ZeroDivisionError): f.result()
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()
def run_sync(self, func): if self._own_loop: return self._io_loop.run_sync(func) else: 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._io_loop.add_callback(run) return f.result()