Reputation: 697
I need to make 3 parallel calls to mongodb collections nameed collection1, collection2 and collection3 and I need to process the data from all together after this. So instead of using sequential calls, I am trying to make concurrent calls using asyncio. I did this in the following way
import asyncio
class Dash:
async def func1():
pipeline1 = []
data = list(collection1.aggregate(pipeline1))
return data
async def func2():
pipeline2 = []
data = list(collection2.aggregate(pipeline2))
return data
async def func2():
pipeline3 = []
data = list(collection3.aggregate(pipeline3))
return data
async def main():
task1 = asyncio.create_task(Dash.func1())
task2 = asyncio.create_task(Dash.func2())
task3 = asyncio.create_task(Dash.func3())
value1 = await task1
value2 = await task2
value3 = await task3
print(value1, value2, value3)
asyncio.run(main())
But the time taken for completing the sequential code and this one is approximately same. few milli seconds difference only. So I checked the order of execution and I find the order of this is exactly same as sequential code. How this can be made in asynchronous way of execution using asyncio (not using motor)
Upvotes: 0
Views: 1031
Reputation: 1276
I think you should use async driver for mongoDB: https://www.mongodb.com/docs/drivers/motor/
Upvotes: 2