如何在 Python 中一次从列表执行多个查询?

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

我正在使用 Tornado 和 Postgres,我有一些查询(4 或 5)在程序期间附加到列表中,现在我想一次执行所有这些查询!

当我尝试执行时出现错误:

"DummyFuture does not support blocking for results" 

我执行了这段代码:

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

"queries" 是查询列表!

这是我的连接池和 Tonado 设置:

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()

不要在 Tornado 协程的 Future 上调用 result()。得到这样的结果:

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

此外,我认为仅将您的查询作为字符串加入是行不通的。结果将无效 SQL。相反:

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