Skip to content

Instantly share code, notes, and snippets.

@hp0404
Last active September 16, 2020 10:52
Show Gist options
  • Select an option

  • Save hp0404/bb3727f696fa6ce83fcd694f42c3e0ed to your computer and use it in GitHub Desktop.

Select an option

Save hp0404/bb3727f696fa6ce83fcd694f42c3e0ed to your computer and use it in GitHub Desktop.
aiohttp, asyncio: An example of how to make concurrent requests with a random timeouts, process JSON response and store it in a tabular format.
import json
import random
import asyncio
import aiohttp
import pandas as pd
SLEEP_RANGE = 0.4, 0.8
CONCURRENT_CONNECTIONS = 5
async def fetch(sem, session, url):
async with sem, session.get(url) as response:
await asyncio.sleep(random.uniform(*SLEEP_RANGE))
data = await response.json()
return {
"ProzorroTenderID": url.split("/")[-1],
"JSON_Value": json.dumps(data.get("data", "error"))
}
async def fetch_all(urls, loop):
sem = asyncio.BoundedSemaphore(CONCURRENT_CONNECTIONS)
async with aiohttp.ClientSession(loop=loop) as session:
results = await asyncio.gather(
*[fetch(sem, session, url) for url in urls]
)
return pd.DataFrame(results)
if __name__ == '__main__':
urls = (
"https://public.api.openprocurement.org/api/2.5/tenders/6a0585fcfb05471796bb2b6a1d379f9b",
"https://public.api.openprocurement.org/api/2.5/tenders/d1c74ec8bb9143d5b49e7ef32202f51c",
"https://public.api.openprocurement.org/api/2.5/tenders/a3ec49c5b3e847fca2a1c215a2b69f8d",
"https://public.api.openprocurement.org/api/2.5/tenders/52d8a15c55dd4f2ca9232f40c89bfa82",
"https://public.api.openprocurement.org/api/2.5/tenders/b3af1cc6554440acbfe1d29103fe0c6a",
"https://public.api.openprocurement.org/api/2.5/tenders/1d1c6560baac4a968f2c82c004a35c90",
)
loop = asyncio.get_event_loop()
data = loop.run_until_complete(fetch_all(urls, loop))
print(data)
@hp0404
Copy link
Author

hp0404 commented Sep 16, 2020

I also learnt that you should never return json as a string or as built-in python dict. Hence I added json.dumps(data.get("data", "error"))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment