Permalink
Browse files

Add gen.WaitAll

  • Loading branch information...
bdarnell committed Sep 4, 2011
1 parent e4ead59 commit 096595233d68940ae0b1f38b25734d9385a978d2
Showing with 30 additions and 0 deletions.
  1. +20 −0 tornado/gen.py
  2. +10 −0 tornado/test/gen_test.py
View
@@ -143,6 +143,26 @@ def is_ready(self):
def get_result(self):
return self.runner.pop_result(self.key)
+class WaitAll(YieldPoint):
+ """Returns the results of multiple previous `Callbacks`.
+
+ The argument is a sequence of `Callback` keys, and the result is
+ a list of results in the same order.
+ """
+ def __init__(self, keys):
+ assert isinstance(keys, list)
@jparise

jparise Sep 4, 2011

Contributor

Any reason not to also allow tuples (or any sequence, for that matter)?

@bdarnell

bdarnell Sep 4, 2011

Owner

Not really. I had originally envisioned a more complicated scheme in which you could pass in a dictionary and get back a parallel dictionary, but that got messy so I dropped it. Tuples should definitely be allowed. Unordered containers like sets are a little weird since there's no way to match up the input and output elements, but that's not enough of a reason to restrict the types of input.

+ self.keys = keys
+
+ def start(self, runner):
+ self.runner = runner
+
+ def is_ready(self):
+ return all(self.runner.is_ready(key) for key in self.keys)
+
+ def get_result(self):
+ return [self.runner.pop_result(key) for key in self.keys]
+
+
class Task(YieldPoint):
"""Runs a single asynchronous operation.
View
@@ -115,6 +115,16 @@ def f():
self.stop()
self.run_gen(f)
+ def test_wait_all(self):
+ @gen.engine
+ def f():
+ (yield gen.Callback("k1"))("v1")
+ (yield gen.Callback("k2"))("v2")
+ results = yield gen.WaitAll(["k1", "k2"])
+ self.assertEqual(results, ["v1", "v2"])
+ self.stop()
+ self.run_gen(f)
+
class GenSequenceHandler(RequestHandler):
@asynchronous

0 comments on commit 0965952

Please sign in to comment.