-
Notifications
You must be signed in to change notification settings - Fork 0
/
concurrency.py
75 lines (58 loc) · 1.65 KB
/
concurrency.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from urllib import request
import time
def _timeit(fn):
t = time.time()
fn()
print('time:', time.time() - t)
def _sync_request_url(url: str) -> bool:
wb_data = request.urlopen(url)
if wb_data.code == 200:
try:
print(time.time(), url, len(wb_data.read()))
except IOError:
return False
return True
else:
return False
URL_SET = ['https://duckduckgo.com/?q={}'.format(i) for i in range(30)]
statistic_30_urls = """
host: 4 cores 3.4Ghz, 16Gb ram, 11.09 МБайт/с IN, 4.10 МБайт/с OUT
proc_pool
10 = 2.25 sec
5 = 3.08 sec
1 = 13.7 sec
thread_pool
10 = 1.63 sec
5 = 2.92 sec
1 = 13.03 sec
aio_loop
3.72 sec
"""
def proc_pool():
from multiprocessing import Pool
with Pool(1) as p:
res = p.map(_sync_request_url, URL_SET)
print(len(res), 'ok')
def thread_pool():
from multiprocessing.pool import ThreadPool
with ThreadPool(1) as p:
res = p.map(_sync_request_url, URL_SET)
print(len(res), 'ok')
def aio_loop():
import aiohttp
import asyncio
async def main():
async with aiohttp.ClientSession() as session:
for url in URL_SET:
async with session.get(url) as response:
# print("Status:", response.status)
# print("Content-type:", response.headers['content-type'])
html = await response.text()
# print("Body:", html[:15], "...")
print(time.time(), url, len(html))
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
if __name__ == '__main__':
_timeit(
aio_loop
)