Search code examples
pythontornado

Tornado: how to spawn two requests and process results as they are done


I am a little lost with Tornado and could use some advice about which way to tackle a problem I am trying to solve. While handling a request, I need to make two calls out to other services and process the results and return to the client as the requests are done rather than waiting for them all to complete. The first result that meets my needs will get returned to the caller.

I have been reading up on tornado.gen, coroutines and futures and am very confused about what will work for this. I do see that I will need to use tornado.gen.WaitIterator() to get the results of futures as they finish. I am not sure how to make the two external requests and how to put it all together.


Solution

  • from tornado import gen, httpclient
    
    async def foo():
        client = httpclient.AsyncHTTPClient()
        waiter = gen.WaitIterator(client.fetch(url1), client.fetch(url2))
        async for resp in waiter:
            # Do something with resp.
            # waiter.current_index tells you which request this is.
    

    If you only want to process the first result, you can return or break from inside the async for loop (although sometimes this spams the logs about futures that were started but never awaited).

    There are more examples in WaitIterator's docs