1.Return 异常
class Return(Exception):
"""Special exception to return a value from a `coroutine`.If this exception is raised, its value argument is used as the
result of the coroutine::@gen.coroutine
def fetch_json(url): response = yield AsyncHTTPClient().fetch(url) raise gen.Return(json_decode(response.body))In Python 3.3, this exception is no longer necessary: the ``return``
statement can be used directly to return a value (previously ``yield`` and ``return`` with a value could not be combined in the same function).By analogy with the return statement, the value argument is optional,
but it is never necessary to ``raise gen.Return()``. The ``return`` statement can be used with no arguments instead. """ def __init__(self, value=None): super(Return, self).__init__() self.value = value # Cython recognizes subclasses of StopIteration with a .args tuple. self.args = (value,)
2.gen.corountine装饰器中捕获异常
try: result = func(*args, **kwargs) except (Return, StopIteration) as e: #捕获了Return异常 result = _value_from_stopiteration(e) except Exception: future.set_exc_info(sys.exc_info()) return future3.使用方式,参考1
@gen.coroutine
def fetch_json(url): response = yield AsyncHTTPClient().fetch(url) raise gen.Return(json_decode(response.body))else:
。。