python-3.xasynchronoussqlalchemyflask-restful

Python 3.6 Flask restful and Async for loop


So, I have flask restful configured with Sqlalchemy which is returning data from the database and I want to process a function call from the returned data asynchronously.

Example

def doSomething(id):
 doing tasks and updating to db


Class XYZ(Resource):
 def get():
  getId = dbgetIds()
  for ids in getId:
   doSomething(ids)

I want to make the for loop call the function asynchronously because each call takes a lot of time and by the time it reaches the end of the list it ready to update again. Can anyone help?


Solution

  • you can use Python's asyncio library along with asyncpg for asynchronous database operations. This approach allows you to run multiple tasks concurrently, improving the performance of your application.

    import asyncio
    import asyncpg
    import time
    
    async def doSomething(id):
        await asyncio.sleep(2)
        conn = await asyncpg.connect(user='postgres', password='kartyk', database='test_db', host='localhost')
        await conn.execute('UPDATE user_data SET password = \'changed\' WHERE user_id = $1', id)
        await conn.close()
    
    class XYZ:
        async def get(self):
            conn = await asyncpg.connect(user='postgres', password='kartyk', database='test_db', host='localhost')
            users = await conn.fetch('SELECT * FROM user_data')
            await conn.close()
    
            tasks = [doSomething(user['user_id']) for user in users]
            await asyncio.gather(*tasks)
    
            return {'status': 'success'}
    
    async def main():
        start = time.perf_counter()
        xyz = XYZ()
        result = await xyz.get()
        print(result)
        end = time.perf_counter()
        
        print(f"Time taken: {end - start} seconds")
    
    if __name__ == "__main__":
        asyncio.run(main())