Created
March 12, 2019 06:15
-
-
Save manashmandal/f579ee3b86afc0c60ee3437d550f4721 to your computer and use it in GitHub Desktop.
Asyncio send concurrent requests and save responses in mongo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
import aiohttp | |
import motor.motor_asyncio | |
client = motor.motor_asyncio.AsyncIOMotorClient() | |
db = client['async_db'] | |
async_collection = db['async_collection'] | |
async def send_request(session): | |
async with session.get('https://jsonplaceholder.typicode.com/todos/1') as resp: | |
# print(resp.status) | |
d = await resp.json() | |
await async_collection.insert_one(d) | |
r = 1000 | |
async def main(): | |
tasks = [] | |
async with aiohttp.ClientSession() as session: | |
for i in range(r): | |
task = asyncio.ensure_future(send_request(session)) | |
tasks.append(task) | |
await asyncio.gather(*tasks) | |
loop = asyncio.get_event_loop() | |
loop.run_until_complete(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment