Search code examples
pythonpostgresqltornado

How to execute multi query from list at once time in Python?


I'm using Tornado and Postgres, I have some queries (4 or 5) that I appended to the list during program and now I want to execute all of them at once time!

when I tried to execute I got error that was:

"DummyFuture does not support blocking for results" 

I executed this code:

 yield self.db.execute(''.join(queries)).result() 

"queries" is list of queries!

This is my connection pool and also Tonado setting:

ioloop = IOLoop.instance()

application.db = momoko.Pool(
    dsn='dbname=xxx user=xxx password=xxxx host=x port=xxxx'
    size=xx,
    ioloop=ioloop,
)

# this is a one way to run ioloop in sync
future = application.db.connect()
ioloop.add_future(future, lambda f: ioloop.stop())
ioloop.start()
future.result()  # raises exception on connection error

http_server = HTTPServer(application)
http_server.listen(8888, 'localhost')
ioloop.start()

Solution

  • Don't call result() on a Future in a Tornado coroutine. Get results like this:

    @gen.coroutine
    def method(self):
        result = yield self.db.execute('...')
    

    Also, I don't think it will work to just join your queries as strings. The outcome won't be valid SQL. Instead:

    @gen.coroutine
    def method(self):
        results = yield [self.db.execute(q) for q in queries]