-
Notifications
You must be signed in to change notification settings - Fork 3
/
activity_3.py
64 lines (49 loc) · 1.82 KB
/
activity_3.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
import math
import urllib.request
from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor
def multithreading(func, args, workers):
with ThreadPoolExecutor(workers) as ex:
res = ex.map(func, args)
return list(res)
def multiprocessing(func, args, workers):
with ProcessPoolExecutor(workers) as ex:
res = ex.map(func, args)
return list(res)
def io_intensive(url):
write_count = 50
with urllib.request.urlopen(url, timeout=20) as conn:
page = conn.read()
for _ in range(write_count):
with open("output.txt", "w") as output:
output.write(str(page))
def test_io_intensive_threads(addrs, n_threads):
print(f" launching multithreading: {n_threads} io")
multithreading(io_intensive, addrs, n_threads)
def compute_intensive(x):
return x * math.cos(x*math.pi)
def test_compute_intensive(n_workers: int=1):
print(f"launching multiprocessing with {n_workers} workers.")
multiprocessing(compute_intensive, range(10**3), n_workers)
return
def main_io(n_threads: int=1):
addrs = [
"https://en.wikipedia.org/wiki/Main_Page",
"https://www.google.com/",
"https://www.kaggle.com/competitions",
"https://www.amazon.com/charts/mostread/fiction/",
"https://www.amazon.com/charts/mostread/nonfiction",
"https://www.amazon.com/charts/mostsold/nonfiction",
"https://www.amazon.com/charts/mostsold/fiction",
"https://www.nytimes.com",
"https://www.bbc.com/",
"https://www.lemonde.fr",
"https://edition.cnn.com",
]
test_io_intensive_threads(addrs, n_threads)
def main_cpu():
test_compute_intensive(n_workers=1)
if __name__ == "__main__":
# print("launching io")
# main_io(n_threads=1)
print("launching cpu")
main_cpu(n_workers=1)