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


Python concurrent.is_future方法代码示例

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


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

示例1: maybe_future

# 需要导入模块: from tornado import concurrent [as 别名]
# 或者: from tornado.concurrent import is_future [as 别名]
def maybe_future(x):
    """Converts ``x`` into a `.Future`.

    If ``x`` is already a `.Future`, it is simply returned; otherwise
    it is wrapped in a new `.Future`.  This is suitable for use as
    ``result = yield gen.maybe_future(f())`` when you don't know whether
    ``f()`` returns a `.Future` or not.

    .. deprecated:: 4.3
       This function only handles ``Futures``, not other yieldable objects.
       Instead of `maybe_future`, check for the non-future result types
       you expect (often just ``None``), and ``yield`` anything unknown.
    """
    if is_future(x):
        return x
    else:
        fut = Future()
        fut.set_result(x)
        return fut 
开发者ID:tao12345666333,项目名称:tornado-zh,代码行数:21,代码来源:gen.py

示例2: convert_yielded

# 需要导入模块: from tornado import concurrent [as 别名]
# 或者: from tornado.concurrent import is_future [as 别名]
def convert_yielded(yielded):
    """Convert a yielded object into a `.Future`.

    The default implementation accepts lists, dictionaries, and Futures.

    If the `~functools.singledispatch` library is available, this function
    may be extended to support additional types. For example::

        @convert_yielded.register(asyncio.Future)
        def _(asyncio_future):
            return tornado.platform.asyncio.to_tornado_future(asyncio_future)

    .. versionadded:: 4.1
    """
    # Lists and dicts containing YieldPoints were handled earlier.
    if isinstance(yielded, (list, dict)):
        return multi(yielded)
    elif is_future(yielded):
        return yielded
    elif isawaitable(yielded):
        return _wrap_awaitable(yielded)
    else:
        raise BadYieldError("yielded unknown object %r" % (yielded,)) 
开发者ID:tao12345666333,项目名称:tornado-zh,代码行数:25,代码来源:gen.py

示例3: _do_all_execute_forasync

# 需要导入模块: from tornado import concurrent [as 别名]
# 或者: from tornado.concurrent import is_future [as 别名]
def _do_all_execute_forasync(self, handler, clear, method_name, **kwargs):

        for c_module in self.common_modules:
            result = self._execute_module(handler, clear, c_module, getattr(c_module, method_name), **kwargs)
            if is_future(result):
                result = yield result
            if result:
                raise gen.Return(1)

        for name, r_module in self.route_modules.items():
            for md in r_module:
                result = self._execute_module(handler, clear, md, getattr(md, method_name), name, **kwargs)
                if is_future(result):
                    result = yield result
                if result:
                    raise gen.Return(1) 
开发者ID:mqingyn,项目名称:torngas,代码行数:18,代码来源:httpmodule.py

示例4: __getattr__

# 需要导入模块: from tornado import concurrent [as 别名]
# 或者: from tornado.concurrent import is_future [as 别名]
def __getattr__(self, name):
        method = self.__api_method_cache.get(name)
        if not method:
            @gen.coroutine
            def method(*args, **kwds):
                client = yield self.get_client()
                api = getattr(client, name, None)
                will_put_back = True
                try:
                    if api and (callable(api) or is_future(api)):
                        raise gen.Return((yield api(*args, **kwds)))
                    raise AttributeError("%s not found in %s" % (name, client))
                except client.TTransportException:
                    will_put_back = False
                    client.close()
                    raise
                finally:
                    if will_put_back:
                        self.put_back_connection(client)

            self.__api_method_cache[name] = method
        return method 
开发者ID:Thriftpy,项目名称:thrift_connector,代码行数:24,代码来源:tornado.py

示例5: __init__

# 需要导入模块: from tornado import concurrent [as 别名]
# 或者: from tornado.concurrent import is_future [as 别名]
def __init__(self, children, quiet_exceptions=()):
        self.keys = None
        if isinstance(children, dict):
            self.keys = list(children.keys())
            children = children.values()
        self.children = []
        for i in children:
            if not isinstance(i, YieldPoint):
                i = convert_yielded(i)
            if is_future(i):
                i = YieldFuture(i)
            self.children.append(i)
        assert all(isinstance(i, YieldPoint) for i in self.children)
        self.unfinished_children = set(self.children)
        self.quiet_exceptions = quiet_exceptions 
开发者ID:tao12345666333,项目名称:tornado-zh,代码行数:17,代码来源:gen.py

示例6: execute_next_for_async

# 需要导入模块: from tornado import concurrent [as 别名]
# 或者: from tornado.concurrent import is_future [as 别名]
def execute_next_for_async(self, request, call_list, process_object, *args, **kwargs):

        while call_list and len(call_list):
            try:
                result = self._execute(request, call_list, process_object, *args, **kwargs)
                if is_future(result):
                    result = yield result
                if result:
                    break
            except NotCallableError:
                break 
开发者ID:mqingyn,项目名称:torngas,代码行数:13,代码来源:manager.py

示例7: handle

# 需要导入模块: from tornado import concurrent [as 别名]
# 或者: from tornado.concurrent import is_future [as 别名]
def handle(self, endpoint, *args, **kwargs):
        """
        almost identical to Resource.handle, except
        the way we handle the return value of view_method.
        """
        method = self.request_method()

        try:
            if not method in self.http_methods.get(endpoint, {}):
                raise MethodNotImplemented(
                    "Unsupported method '{}' for {} endpoint.".format(
                        method,
                        endpoint
                    )
                )

            if not self.is_authenticated():
                raise Unauthorized()

            self.data = self.deserialize(method, endpoint, self.request_body())
            view_method = getattr(self, self.http_methods[endpoint][method])
            data = view_method(*args, **kwargs)
            if is_future(data):
                # need to check if the view_method is a generator or not
                data = yield data
            serialized = self.serialize(method, endpoint, data)
        except Exception as err:
            raise gen.Return(self.handle_error(err))

        status = self.status_map.get(self.http_methods[endpoint][method], OK)
        raise gen.Return(self.build_response(serialized, status=status)) 
开发者ID:toastdriven,项目名称:restless,代码行数:33,代码来源:tnd.py

示例8: call

# 需要导入模块: from tornado import concurrent [as 别名]
# 或者: from tornado.concurrent import is_future [as 别名]
def call(self, method, *args, **kwargs):
        """
        Execute synchronous or asynchronous method of client and return the
        result.
        """
        result = getattr(self.client, method)(*args, **kwargs)
        if is_future(result):
            result = yield result
        raise tornado.gen.Return(result) 
开发者ID:uber-common,项目名称:opentracing-python-instrumentation,代码行数:11,代码来源:test_traced_function_decorator.py

示例9: multi_future

# 需要导入模块: from tornado import concurrent [as 别名]
# 或者: from tornado.concurrent import is_future [as 别名]
def multi_future(children, quiet_exceptions=()):
    """Wait for multiple asynchronous futures in parallel.

    This function is similar to `multi`, but does not support
    `YieldPoints <YieldPoint>`.

    .. versionadded:: 4.0

    .. versionchanged:: 4.2
       If multiple ``Futures`` fail, any exceptions after the first (which is
       raised) will be logged. Added the ``quiet_exceptions``
       argument to suppress this logging for selected exception types.

    .. deprecated:: 4.3
       Use `multi` instead.
    """
    if isinstance(children, dict):
        keys = list(children.keys())
        children = children.values()
    else:
        keys = None
    children = list(map(convert_yielded, children))
    assert all(is_future(i) for i in children)
    unfinished_children = set(children)

    future = Future()
    if not children:
        future.set_result({} if keys is not None else [])

    def callback(f):
        unfinished_children.remove(f)
        if not unfinished_children:
            result_list = []
            for f in children:
                try:
                    result_list.append(f.result())
                except Exception as e:
                    if future.done():
                        if not isinstance(e, quiet_exceptions):
                            app_log.error("Multiple exceptions in yield list",
                                          exc_info=True)
                    else:
                        future.set_exc_info(sys.exc_info())
            if not future.done():
                if keys is not None:
                    future.set_result(dict(zip(keys, result_list)))
                else:
                    future.set_result(result_list)

    listening = set()
    for f in children:
        if f not in listening:
            listening.add(f)
            f.add_done_callback(callback)
    return future 
开发者ID:tao12345666333,项目名称:tornado-zh,代码行数:57,代码来源:gen.py

示例10: run_sync

# 需要导入模块: from tornado import concurrent [as 别名]
# 或者: from tornado.concurrent import is_future [as 别名]
def run_sync(self, func, timeout=None):
        """Starts the `IOLoop`, runs the given function, and stops the loop.

        The function must return either a yieldable object or
        ``None``. If the function returns a yieldable object, the
        `IOLoop` will run until the yieldable is resolved (and
        `run_sync()` will return the yieldable's result). If it raises
        an exception, the `IOLoop` will stop and the exception will be
        re-raised to the caller.

        The keyword-only argument ``timeout`` may be used to set
        a maximum duration for the function.  If the timeout expires,
        a `TimeoutError` is raised.

        This method is useful in conjunction with `tornado.gen.coroutine`
        to allow asynchronous calls in a ``main()`` function::

            @gen.coroutine
            def main():
                # do stuff...

            if __name__ == '__main__':
                IOLoop.current().run_sync(main)

        .. versionchanged:: 4.3
           Returning a non-``None``, non-yieldable value is now an error.
        """
        future_cell = [None]

        def run():
            try:
                result = func()
                if result is not None:
                    from tornado.gen import convert_yielded
                    result = convert_yielded(result)
            except Exception:
                future_cell[0] = TracebackFuture()
                future_cell[0].set_exc_info(sys.exc_info())
            else:
                if is_future(result):
                    future_cell[0] = result
                else:
                    future_cell[0] = TracebackFuture()
                    future_cell[0].set_result(result)
            self.add_future(future_cell[0], lambda future: self.stop())
        self.add_callback(run)
        if timeout is not None:
            timeout_handle = self.add_timeout(self.time() + timeout, self.stop)
        self.start()
        if timeout is not None:
            self.remove_timeout(timeout_handle)
        if not future_cell[0].done():
            raise TimeoutError('Operation timed out after %s seconds' % timeout)
        return future_cell[0].result() 
开发者ID:tao12345666333,项目名称:tornado-zh,代码行数:56,代码来源:ioloop.py


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