来自tornado.gen documentation有人可以帮我理解tornado.gen.coroutine和tornado.gen.engine
之间的确切区别答案 0 :(得分:6)
正如gen.engine
的龙卷风文档所说:
这个装饰器类似于coroutine,除了它不返回一个 Future和回调参数没有特别对待。
正如gen.coroutine
文档所说
从调用者的角度来看,@ gen.coroutine类似于 @return_future和@ gen.engine的组合。
gen.engine
基本上是一个比coroutine更老的,更简化的版本。如果您要编写新代码,则应遵循文档提供的建议并始终使用tornado.gen.coroutine
。
如果你查看两个函数的代码(删除了文档),这很明显。
发动机:
def engine(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
runner = None
def handle_exception(typ, value, tb):
if runner is not None:
return runner.handle_exception(typ, value, tb)
return False
with ExceptionStackContext(handle_exception) as deactivate:
try:
result = func(*args, **kwargs)
except (Return, StopIteration) as e:
result = getattr(e, 'value', None)
else:
if isinstance(result, types.GeneratorType):
def final_callback(value):
if value is not None:
raise ReturnValueIgnoredError(
"@gen.engine functions cannot return values: "
"%r" % (value,))
assert value is None
deactivate()
runner = Runner(result, final_callback)
runner.run()
return
if result is not None:
raise ReturnValueIgnoredError(
"@gen.engine functions cannot return values: %r" %
(result,))
deactivate()
# no yield, so we're done
return wrapper
协程:
def coroutine(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
runner = None
future = TracebackFuture()
if 'callback' in kwargs:
callback = kwargs.pop('callback')
IOLoop.current().add_future(
future, lambda future: callback(future.result()))
def handle_exception(typ, value, tb):
try:
if runner is not None and runner.handle_exception(typ, value, tb):
return True
except Exception:
typ, value, tb = sys.exc_info()
future.set_exc_info((typ, value, tb))
return True
with ExceptionStackContext(handle_exception) as deactivate:
try:
result = func(*args, **kwargs)
except (Return, StopIteration) as e:
result = getattr(e, 'value', None)
except Exception:
deactivate()
future.set_exc_info(sys.exc_info())
return future
else:
if isinstance(result, types.GeneratorType):
def final_callback(value):
deactivate()
future.set_result(value)
runner = Runner(result, final_callback)
runner.run()
return future
deactivate()
future.set_result(result)
return future
return wrapper
乍一看,这些都可能很难理解。但是,显而易见的是,代码非常相似,只是@gen.coroutine
对callback
kwarg有一些特殊处理,并且它构建/返回Future
。 @gen.engine
有一些代码,如果您尝试从中返回某些内容,而不是将其放在Future
中,则会专门抛出错误。