How to return value from async method in python?

I have the following flask application:

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

When I run this code, I got TypeError: 'NoneType' object is not iterable . How to return my values converted to JSON?

+5
source share
1 answer

You need to return your values ​​in your run function so that they are available in test :

 async def run(): conn = await asyncpg.connect(db_url) values = await conn.fetch('''SELECT ... FROM ... WHERE ...;''') await conn.close() return values 
+5
source

Source: https://habr.com/ru/post/1265548/


All Articles