當前位置: 首頁>>代碼示例>>Python>>正文


Python backports_abc.Generator方法代碼示例

本文整理匯總了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 
開發者ID:leohowell,項目名稱:zigmo,代碼行數:28,代碼來源:tornado_style.py

示例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 
開發者ID:leohowell,項目名稱:zigmo,代碼行數:33,代碼來源:concurrent.py


注:本文中的backports_abc.Generator方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。