-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaiohttp_client.py
35 lines (26 loc) · 967 Bytes
/
aiohttp_client.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
import aiohttp
import asyncio
import datetime
BASE_URL = 'http://0.0.0.0:8080'
a = f'{BASE_URL}/1/'
b = f'{BASE_URL}/2/'
start = datetime.datetime.now()
async def get(url, print_=False):
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
if print_:
t = '{0:%H:%M:%S}'.format(datetime.datetime.now())
print('Done: {}, {} ({})'.format(t, response.url, response.status))
print(await response.read())
loop = asyncio.get_event_loop()
tasks = [
asyncio.ensure_future(get(a)),
asyncio.ensure_future(get(a)),
asyncio.ensure_future(get(a)),
asyncio.ensure_future(get(b)),
asyncio.ensure_future(get(b)),
asyncio.ensure_future(get(b))
]
tasks1001 = [asyncio.ensure_future(get(a)) for _ in range(500)] + [asyncio.ensure_future(get(a, print_=True))]
loop.run_until_complete(asyncio.wait(tasks1001))
print(datetime.datetime.now() - start)