當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。