Skip to content

Instantly share code, notes, and snippets.

@manashmandal
Created March 12, 2019 06:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manashmandal/f579ee3b86afc0c60ee3437d550f4721 to your computer and use it in GitHub Desktop.
Save manashmandal/f579ee3b86afc0c60ee3437d550f4721 to your computer and use it in GitHub Desktop.
Asyncio send concurrent requests and save responses in mongo
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