Python 对龙卷风的共同路线感到困惑
我正试图找出龙卷风的py世代中流动的代码Python 对龙卷风的共同路线感到困惑,python,tornado,coroutine,Python,Tornado,Coroutine,我正试图找出龙卷风的py世代中流动的代码 future = TracebackFuture() if replace_callback and 'callback' in kwargs: callback = kwargs.pop('callback') IOLoop.current().add_future( future, lambda future: callback(future.result())) tr
future = TracebackFuture()
if replace_callback and 'callback' in kwargs:
callback = kwargs.pop('callback')
IOLoop.current().add_future(
future, lambda future: callback(future.result()))
try:
result = func(*args, **kwargs)
except (Return, StopIteration) as e:
result = getattr(e, 'value', None)
except Exception:
future.set_exc_info(sys.exc_info())
return future
else:
if isinstance(result, types.GeneratorType):
try:
orig_stack_contexts = stack_context._state.contexts
yielded = next(result) # A (mark)
if stack_context._state.contexts is not orig_stack_contexts:
yielded = TracebackFuture()
yielded.set_exception(
stack_context.StackContextInconsistentError(
'stack_context inconsistency (probably caused '
'by yield within a "with StackContext" block)'))
except (StopIteration, Return) as e:
future.set_result(getattr(e, 'value', None))
except Exception:
future.set_exc_info(sys.exc_info())
else:
Runner(result, future, yielded)
try:
return future
finally:
future = None
future.set_result(result) # B
return future
我有两个功能
@coroutine
def foo():
#do something
bar = yield bar()
#do something else
@coroutine
def bar():
#do something
return [1,2,3,4]
然后,调用foo()
函数,代码运行到标记为A的点。函数bar()
将被调用,因为bar()
不是GeneratorType,它将像普通函数一样运行。接下来,设置future结果并返回future。在返回未来之前设置结果的这一点是最令人困惑的。在bar()
返回未来之后,foo()
获取此未来,但该未来在被io\u循环处理之前设置了\u结果。添加\u future
任何帮助都将不胜感激,提前感谢。我太愚蠢了,竟然忽略了代码:
if self.handle_yield(first_yielded):
self.run()
如果未来完成,值将发送到相应的生成器