Concurrent Queries in TornadoΒΆ

The following example issues multiple concurrent queries in a single asynchronous request and will wait until all queries are complete before progressing:

from tornado import gen, ioloop, web
import queries

class RequestHandler(web.RequestHandler):

    def initialize(self):
        self.session = queries.TornadoSession()

    def get(self, *args, **kwargs):

        # Issue the three queries and wait for them to finish before progressing
         q3result) = yield [self.session.query('SELECT * FROM foo'),
                            self.session.query('SELECT * FROM bar'),
                            self.session.query('INSERT INTO requests VALUES (%s, %s, %s)',
                                                self.headers.get('User-Agent', '')])]
        # Close the connection
        self.finish({'q1result': q1result.items(),
                     'q2result': q2result.items()})

        # Free the results and connection locks

if __name__ == "__main__":
    application = web.Application([
        (r"/", RequestHandler)