From 15e2812af75f1f2b53876f90cc366fdafe4944cb Mon Sep 17 00:00:00 2001 From: Anton Ryzhov Date: Thu, 17 Oct 2013 19:44:09 +0400 Subject: [PATCH] Documentation for yielding dicts --- docs/gen.rst | 3 ++- tornado/gen.py | 8 ++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/docs/gen.rst b/docs/gen.rst index 28879c00..d09a9573 100644 --- a/docs/gen.rst +++ b/docs/gen.rst @@ -18,7 +18,8 @@ their result method will be called automatically when they are ready. Additionally, lists of any combination of these objects may be yielded; the result is a list of the results of each yield point - in the same order. + in the same order. Yielding dicts with these objects in values will + return dict with results at the same keys. .. autoclass:: Task diff --git a/tornado/gen.py b/tornado/gen.py index 7eb2c0ca..a363a420 100644 --- a/tornado/gen.py +++ b/tornado/gen.py @@ -38,8 +38,8 @@ since it is both shorter and provides better exception handling):: def get(self): yield gen.Task(AsyncHTTPClient().fetch, "http://example.com") -You can also yield a list of ``Futures`` and/or ``Tasks``, which will be -started at the same time and run in parallel; a list of results will +You can also yield a list or dict of ``Futures`` and/or ``Tasks``, which will be +started at the same time and run in parallel; a list or dict of results will be returned when they are all finished:: @gen.coroutine @@ -47,6 +47,10 @@ be returned when they are all finished:: http_client = AsyncHTTPClient() response1, response2 = yield [http_client.fetch(url1), http_client.fetch(url2)] + response_dict = yield dict(response3=http_client.fetch(url3), + response4=http_client.fetch(url4)) + response3 = response_dict['response3'] + response4 = response_dict['response4'] For more complicated interfaces, `Task` can be split into two parts: `Callback` and `Wait`::