-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path07-asyncio.py
46 lines (35 loc) · 1.28 KB
/
07-asyncio.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import asyncio
from aiohttp import ClientSession
import requests
errors = []
async def fetch(url, session):
try:
async with session.get(url) as response:
return await response.json()
except Exception as e:
errors.append(str(e))
async def bound_fetch(sem, url, session):
# Getter function with semaphore.
async with sem:
return await fetch(url, session)
async def run(loop):
resp = requests.get('https://hacker-news.firebaseio.com/v0/topstories.json')
top_stories = resp.json()
tasks = []
# create instance of Semaphore
sem = asyncio.Semaphore(50)
# Create client session that will ensure we dont open new connection
# per each request.
async with ClientSession() as session:
for item_id in top_stories:
url = 'https://hacker-news.firebaseio.com/v0/item/{}.json'.format(item_id)
# pass Semaphore and session to every GET request
task = asyncio.ensure_future(bound_fetch(sem, url, session))
tasks.append(task)
responses = await asyncio.gather(*tasks)
for data in responses:
if data is not None:
print(data['title'])
loop = asyncio.get_event_loop()
future = asyncio.ensure_future(run(loop))
loop.run_until_complete(future)