本文整理汇总了Python中backports_abc.Generator方法的典型用法代码示例。如果您正苦于以下问题:Python backports_abc.Generator方法的具体用法?Python backports_abc.Generator怎么用?Python backports_abc.Generator使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类backports_abc
的用法示例。
在下文中一共展示了backports_abc.Generator方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: coroutine
# 需要导入模块: import backports_abc [as 别名]
# 或者: from backports_abc import Generator [as 别名]
def coroutine(func):
def wrapper(*args, **kwargs):
def _dispatch(yielded):
if isinstance(yielded, GeneratorType):
return _execute_yield(yielded)
else:
return _send(yielded)
def _send(yielded):
try:
yielded = origin_gen.send(yielded)
return _dispatch(yielded)
except (StopIteration, Return) as e:
return getattr(e, 'value', None)
except Exception as error:
print 'terrible error happened: %r' % error
def _execute_yield(gen):
yielded = next(gen)
return _dispatch(yielded)
result = func(*args, **kwargs)
origin_gen = result
return _execute_yield(result)
return wrapper
示例2: coroutine
# 需要导入模块: import backports_abc [as 别名]
# 或者: from backports_abc import Generator [as 别名]
def coroutine(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
future = Future()
try:
result = func(*args, **kwargs)
except (StopIteration, Return) as e:
result = getattr(e, 'value', None)
except Exception:
future.exc_info = sys.exc_info()
return future
else:
if isinstance(result, GeneratorType):
try:
yielded = next(result)
except (StopIteration, Return) as e:
future.set_result(getattr(e, 'value', None))
except Exception:
future.exc_info = sys.exc_info()
else:
# result is generator, yielded is Future
Runner(result, future, yielded)
try:
return future
finally:
future = None
future.result = result
future.done = True
return future
return wrapper