-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequests.py
More file actions
59 lines (48 loc) · 1.7 KB
/
Copy pathrequests.py
File metadata and controls
59 lines (48 loc) · 1.7 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
import asyncio
import requests
async def get_request(url):
data = requests.get_request(url)
return url
async def waiter(event):
print('> waiting for it ...')
await event.wait()
print('> ... got it!')
# async def main():
# # Create an Event object.
# event = asyncio.Event()
#
# # Spawn a Task to wait until 'event' is set.
# waiter_task = asyncio.create_task(waiter(event))
# print('going to sleep')
# # Sleep for 1 second and set the event.
# await asyncio.sleep(1)
# event.set()
# print('event set')
# # Wait until the waiter task is finished.
# await waiter_task
import asyncio
import aiohttp # pip install aiohttp aiodns
async def get(session: aiohttp.ClientSession, query: str):
url = f"http://humanyze.com"
print(f"Requesting {url}")
resp = await session.request('GET', url=url)
# Note that this may raise an exception for non-2xx responses
print(resp, resp.__class__)
# data = await resp.json(content_type='text/html')
print(f"Received data for {url}")
return resp
async def main(colors):
# Asynchronous context manager. Prefer this rather
# than using a different session for each GET request
async with aiohttp.ClientSession() as session:
tasks = []
for c in colors:
tasks.append(get(session=session, query=c))
# asyncio.gather() will wait on the entire task set to be completed
htmls = [await _ for _ in asyncio.as_completed(tasks)]
print(f'htmls -> {htmls}')
return htmls
if __name__ == '__main__':
colors = ['red', 'blue', 'green'] # ...
# Either take colors from stdin or make some default here
asyncio.run(main(colors)) # Python 3.7+