Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/473.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在Python Tornado中相当于jquery$_Javascript_Jquery_Python_Tornado - Fatal编程技术网

Javascript 在Python Tornado中相当于jquery$

Javascript 在Python Tornado中相当于jquery$,javascript,jquery,python,tornado,Javascript,Jquery,Python,Tornado,在jQuery中,$。当(promise1,promise2…用作主承诺时,表示其子承诺的总体状态。然后,我们可以将.done(callback)附加到$。当承诺时,当所有承诺1、承诺2…完成时,将执行回调 在Python(Tornado)中,Future的行为类似于javascript中的promise,而AsyncHTTPClient中的fetch()返回未来 在下面的代码中,我有一个未来列表 from tornado.httpclient import AsyncHTTPClient h

在jQuery中,
$。当(promise1,promise2…
用作主承诺时,表示其子承诺的总体状态。然后,我们可以将
.done(callback)
附加到
$。当
承诺时,当所有
承诺1、承诺2…
完成时,将执行
回调

在Python(Tornado)中,
Future
的行为类似于javascript中的promise,而
AsyncHTTPClient
中的
fetch()
返回未来

在下面的代码中,我有一个未来列表

from tornado.httpclient import AsyncHTTPClient

httpclient = AsyncHTTPClient()
futures = [
    httpclient.fetch("http://google.com")
    httpclient.fetch("http://example.com")
    httpclient.fetch("http://example.org")
]

def all_futures_done_callback():
    ...

当所有期货完成时,我如何执行
all\u futures\u done\u回调

在我看来,您需要自己构建此功能。这是未经测试的,但像这样的东西似乎应该会起作用:

class FutureCollection(Future):
    def __init__(self, *args, **kwargs):
        super(FutureCollection, self).__init__(*args, **kwargs)
        self._waiting_for = []

    def _check_all_done_and_resolve(self, future):
        if all(f.done() for f in self._waiting_for):
            # Maybe check for exceptions a. la.
            # http://tornado.readthedocs.org/en/latest/_modules/tornado/concurrent.html#chain_future
            self.set_result(None)  # Not sure what the result should be.

    def add_future(self, future):
        self._waiting_for.append(future)
        future.add_done_callback(self._check_all_done_and_resolve)

    @property
    def futures(self):
        # Read-only access to the futures that have been added.
        return iter(self._waiting_for)

在协同过程中,很容易等待多个未来;只需将它们列为一个列表:

@gen.coroutine
def f():
    futures = [
        httpclient.fetch("http://google.com")
        httpclient.fetch("http://example.com")
        httpclient.fetch("http://example.org")
    ]
    responses = yield futures

要使用回调而不是协同程序来实现这一点,您需要类似于mgilson的答案。

Ben的答案很简洁,但您的答案更具可扩展性,因此我接受了您的答案。谢谢你们两位@说句公道话,我以前从未用过龙卷风;-)。最近我对JS体验中的承诺有点熟悉,所以我想我应该用另一种语言试试。