本文整理匯總了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