我有以下 flask 应用程序:
async def run():
conn = await asyncpg.connect(db_url)
values = await conn.fetch('''SELECT ... FROM ... WHERE ...;''')
await conn.close()
@app.route('/')
def test():
loop = asyncio.get_event_loop()
res = loop.run_until_complete(run())
return json.dumps([dict(r) for r in res])
if __name__ == '__main__':
app.run()
当我运行这段代码时,我得到了 TypeError: 'NoneType' object is not iterable
。如何返回转换为 JSON 的 values
?
最佳答案
您需要在 run
函数中返回您的值,以便它们在 test
中可用:
async def run():
conn = await asyncpg.connect(db_url)
values = await conn.fetch('''SELECT ... FROM ... WHERE ...;''')
await conn.close()
return values
关于python - 如何从python中的异步方法返回值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42832543/