forked from NastyaBay/multi-task-at-18
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIO-bound.py
More file actions
22 lines (17 loc) · 683 Bytes
/
IO-bound.py
File metadata and controls
22 lines (17 loc) · 683 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import urllib.request
from urllib.parse import unquote
import concurrent.futures
links = open('res.txt', encoding='utf8').read().split('\n')
def load_url(link, timeout):
with urllib.request.urlopen(link, timeout=timeout) as conn:
return conn.code
with concurrent.futures.ThreadPoolExecutor(max_workers=100) as executor:
future_to_url = {executor.submit(load_url, url, 5): url for url in links}
for future in concurrent.futures.as_completed(future_to_url):
url = future_to_url[future]
try:
data = future.result()
except Exception as e:
print('%r exception: %s' % (url, e))
else:
print(data)