当前位置: 首页>>代码示例>>Python>>正文


Python TracebackFuture.result方法代码示例

本文整理汇总了Python中tornado.concurrent.TracebackFuture.result方法的典型用法代码示例。如果您正苦于以下问题:Python TracebackFuture.result方法的具体用法?Python TracebackFuture.result怎么用?Python TracebackFuture.result使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在tornado.concurrent.TracebackFuture的用法示例。


在下文中一共展示了TracebackFuture.result方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: wrapper

# 需要导入模块: from tornado.concurrent import TracebackFuture [as 别名]
# 或者: from tornado.concurrent.TracebackFuture import result [as 别名]
    def wrapper(*args, **kwargs):
        future = TracebackFuture()
#        ipdb.set_trace()
        print args, kwargs
#        callback, args, kwargs = replacer.replace(lambda value=_NO_RESULT: future.set_result(value),args, kwargs)
        callback = None
        kwargs['callback'] = lambda value = _NO_RESULT: future.set_result(value)
        print callback, args, kwargs
        def handle_error(typ, value, tb):
            future.set_exc_info((typ, value, tb))
            return True

        exc_info = None
        with ExceptionStackContext(handle_error):
            try:
                result = f(*args, **kwargs)
                if result is not None:
                    raise ReturnValueIgnoredError('@return_future should not be used with function that return values')
            except:
                exc_info = sys.exc_info()
                raise

        if exc_info is not None:
            future.result()

        if callback is not None:
            def run_callback(future):
                result = future.result()
                if result is _NO_RESULT:
                    callback('result is NULL')
                else:
                    callback(future.result())
            future.add_done_callback(wrap(run_callback))
#        print future.result(), 'in the wapper'
        return future
开发者ID:zhaosir,项目名称:Demo,代码行数:37,代码来源:test_gen.py

示例2: wrapper

# 需要导入模块: from tornado.concurrent import TracebackFuture [as 别名]
# 或者: from tornado.concurrent.TracebackFuture import result [as 别名]
    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):
            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):
                    def final_callback(value):
                        future.set_result(value)
                    runner = Runner(result, final_callback)
                    runner.run()
                    return future
            future.set_result(result)
        return future
开发者ID:AlwinHummels,项目名称:CouchPotatoServer,代码行数:36,代码来源:gen.py

示例3: wrapper

# 需要导入模块: from tornado.concurrent import TracebackFuture [as 别名]
# 或者: from tornado.concurrent.TracebackFuture import result [as 别名]
    def wrapper(*args, **kwargs):
        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 = _value_from_stopiteration(e)
        except Exception:
            future.set_exc_info(sys.exc_info())
            return future
        else:
            if isinstance(result, GeneratorType):
                # Inline the first iteration of Runner.run.  This lets us
                # avoid the cost of creating a Runner when the coroutine
                # never actually yields, which in turn allows us to
                # use "optional" coroutines in critical path code without
                # performance penalty for the synchronous case.
                try:
                    orig_stack_contexts = stack_context._state.contexts
                    yielded = next(result)
                    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(_value_from_stopiteration(e))
                except Exception:
                    future.set_exc_info(sys.exc_info())
                else:
                    _futures_to_runners[future] = Runner(result, future, yielded)
                try:
                    return future
                finally:
                    # Subtle memory optimization: if next() raised an exception,
                    # the future's exc_info contains a traceback which
                    # includes this stack frame.  This creates a cycle,
                    # which will be collected at the next full GC but has
                    # been shown to greatly increase memory usage of
                    # benchmarks (relative to the refcount-based scheme
                    # used in the absence of cycles).  We can avoid the
                    # cycle by clearing the local variable after we return it.
                    future = None
        future.set_result(result)
        return future
开发者ID:maxos54,项目名称:ServerSync,代码行数:54,代码来源:gen.py

示例4: wrapper

# 需要导入模块: from tornado.concurrent import TracebackFuture [as 别名]
# 或者: from tornado.concurrent.TracebackFuture import result [as 别名]
    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  # 这样就吃掉exception了?
            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)  # result是func调用返回的一个结果,有可能是一个iterator,比如我们的get、post方法里面塞几个yield的调用
            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)

                    # 这里的result很可能是一个iterator,看看头部的调用例子
                    runner = Runner(result, final_callback) # 关键点
                    runner.run()

                    return future

            deactivate()
            future.set_result(result)
        return future
开发者ID:zhkzyth,项目名称:tornado-reading-notes,代码行数:46,代码来源:gen.py

示例5: wrapper

# 需要导入模块: from tornado.concurrent import TracebackFuture [as 别名]
# 或者: from tornado.concurrent.TracebackFuture import result [as 别名]
    def wrapper(*args, **kwargs):
        future = TracebackFuture()

        if replace_callback and 'callback' in kwargs:
            callback = kwargs.pop('callback')
            IOLoop.current().add_future(
                future, lambda future: callback(future.result()))

        try:
            with Measure(func):
                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

                    with Measure(result, first=True):
                        yielded = next(result)

                    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)
        return future
开发者ID:alexander-gridnev,项目名称:profiletor,代码行数:44,代码来源:__init__.py

示例6: wrapper

# 需要导入模块: from tornado.concurrent import TracebackFuture [as 别名]
# 或者: from tornado.concurrent.TracebackFuture import result [as 别名]
    def wrapper(*args, **kwargs):
        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):
                # Inline the first iteration of Runner.run.  This lets us
                # avoid the cost of creating a Runner when the coroutine
                # never actually yields, which in turn allows us to
                # use "optional" coroutines in critical path code without
                # performance penalty for the synchronous case.
                try:
                    orig_stack_contexts = stack_context._state.contexts
                    yielded = next(result)
                    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)
                return future
        future.set_result(result)
        return future
开发者ID:BingQiangChen,项目名称:tornado,代码行数:43,代码来源:gen.py

示例7: wrapper

# 需要导入模块: from tornado.concurrent import TracebackFuture [as 别名]
# 或者: from tornado.concurrent.TracebackFuture import result [as 别名]
    def wrapper(*args, **kwargs):
        runner = None
        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):
                runner = Runner(result, future)
                runner.run()
                return future
        future.set_result(result)
        return future
开发者ID:EliseCheng,项目名称:tornado,代码行数:25,代码来源:gen.py

示例8: wrapper

# 需要导入模块: from tornado.concurrent import TracebackFuture [as 别名]
# 或者: from tornado.concurrent.TracebackFuture import result [as 别名]
    def wrapper(*args, **kwargs):
        future = TracebackFuture()  # 一个future的跟踪对象,这个是关键

        # 原来的func不支持callback的参数,修饰之后是可以使用的
        # 下面的这个future好好读懂,然后好好理解传说中的future功能
        if replace_callback and 'callback' in kwargs:
            callback = kwargs.pop('callback')

            # 就是future成功了,执行lambda future: callback(future.result())函数,参数就是这个future
            # 也就是把这个可能执行的语句放到IOLoop里面去,这个就是这个add_future的功能
            IOLoop.current().add_future(
                future, lambda future: callback(future.result()))

        # 异步的HTTP和异步的IOStream也许返回的类型不一样,
        # 感觉两个异步的东西才是tornado的关键
        try:
            # 尝试运行函数,这里会yield出来,所以不会被阻塞的
            # 1. 返回一个Return 2. 返回一个StopIteration(这个不是很清楚??)
            # 3. 返回一个Exception?就是运行错误了
            result = func(*args, **kwargs)
        except (Return, StopIteration) as e:
            # 到这里说明结果已经出来了,future的任务结束了
            # ?StopIteration无法理解
            result = getattr(e, 'value', None)
        except Exception:  # 如果出错的话
            # 运行出错了,这个时候依旧会添加到IOLoop._callbacks
            # 下面这句话执行这个future全部的回调
            future.set_exc_info(sys.exc_info())
            return future  # 一但结果确定,直接返回一个future
        else:
            # 如果内部有yield,直接返回一个types.GeneratorType
            if isinstance(result, types.GeneratorType):
                # Inline the first iteration of Runner.run.  This lets us
                # avoid the cost of creating a Runner when the coroutine
                # never actually yields, which in turn allows us to
                # use "optional" coroutines in critical path code without
                # performance penalty for the synchronous case.
                try:
                    orig_stack_contexts = stack_context._state.contexts

                    # 这里是获取yield的结果的地方,一般还是一个future的东西
                    yielded = next(result)  # 获取yield的结果,这里一般是一个future类型yield出来的东西

                    # 先忽略下面的出错处理
                    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:  # bug了
                    future.set_exc_info(sys.exc_info())
                else:  # 返回一个future
                    Runner(result, future, yielded)

                # 如果是一个Exception,这里记录到future,然后返回了
                return future
        future.set_result(result)  # 这个时候添加回调到了IOLoop
        return future
开发者ID:ColorFuzzy,项目名称:tornado_code,代码行数:63,代码来源:gen.py

示例9: AsyncResult

# 需要导入模块: from tornado.concurrent import TracebackFuture [as 别名]
# 或者: from tornado.concurrent.TracebackFuture import result [as 别名]
class AsyncResult(object):
    """
    Essentially a wrapper for a Tornado TracebackFuture
    """
    def __init__(self, future=None):
        if future is not None:
            self._future = future
        else:
            self._future = TracebackFuture()
            self._future.add_done_callback(functools.partial(async_result_complete, self))
        self._condition = threading.Condition()

    def ready(self):
        """Return `True` if and only if it holds a value or an
        exception"""
        return self._future.done()

    def successful(self):
        """Return `True` if and only if it is ready and holds a
        value"""
        return self._future.exception() is None

    @property
    def exception(self):
        return self._future.exception()

    def set(self, value=None):
        """Store the value. Wake up the waiters.

        :param value: Value to store as the result.

        Any waiters blocking on :meth:`get` or :meth:`wait` are woken
        up. Sequential calls to :meth:`wait` and :meth:`get` will not
        block at all."""
        with self._condition:
            self._future.set_result(value)

    def set_exception(self, exception):
        """Store the exception. Wake up the waiters.

        :param exception: Exception to raise when fetching the value.

        Any waiters blocking on :meth:`get` or :meth:`wait` are woken
        up. Sequential calls to :meth:`wait` and :meth:`get` will not
        block at all."""
        with self._condition:
            self._future.set_exception(exception)

    def get(self, block=True, timeout=None):
        """Return the stored value or raise the exception

        :param block: Whether this method should block or return
                      immediately.
        :type block: bool
        :param timeout: How long to wait for a value when `block` is
                        `True`.
        :type timeout: float

        If this instance already holds a value / an exception, return /
        raise it immediately. Otherwise, block until :meth:`set` or
        :meth:`set_exception` has been called or until the optional
        timeout occurs."""
        with self._condition:
            if self.ready():
                return self._future.result()
            elif block:
                self._condition.wait(timeout)
                return self._future.result()

            # if we get to this point we timeout
            raise KazooTimeoutError()

    def get_nowait(self):
        """Return the value or raise the exception without blocking.

        If nothing is available, raise the Timeout exception class on
        the associated :class:`IHandler` interface."""
        return self._future.result()

    def wait(self, timeout=None):
        """Block until the instance is ready.

        :param timeout: How long to wait for a value
        :type timeout: float

        If this instance already holds a value / an exception, return /
        raise it immediately. Otherwise, block until :meth:`set` or
        :meth:`set_exception` has been called or until the optional
        timeout occurs."""
        with self._condition:
            self._condition.wait(timeout)
        return self.ready()

    def rawlink(self, callback):
        """Register a callback to call when a value or an exception is
        set

        :param callback:
            A callback function to call after :meth:`set` or
            :meth:`set_exception` has been called. This function will
#.........这里部分代码省略.........
开发者ID:tristeng,项目名称:kazoo,代码行数:103,代码来源:tornado.py


注:本文中的tornado.concurrent.TracebackFuture.result方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。