-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7_process_pool_executor.py
More file actions
65 lines (48 loc) · 1.81 KB
/
7_process_pool_executor.py
File metadata and controls
65 lines (48 loc) · 1.81 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
60
61
62
63
64
65
from concurrent.futures import ProcessPoolExecutor, as_completed
import urllib.request
import certifi
import os
from multiprocessing import current_process, Process
from time import time
def visit(url, timeout):
with urllib.request.urlopen(url=url, timeout=timeout, cafile=certifi.where()) as conn:
print(f"{url}: {len(conn.read())}Bytes")
def count(process):
process_name = current_process().name
pid = os.getpid()
for i in range(10000):
print(f"Process:{process} Process Name:{process_name} PID:{pid}, work = {i}\n", end="")
return process
def main():
# Visit url lists
urls = ["http://bcit.ca", "http://www.ubc.ca/", "http://www.sfu.ca/", "http://www.stanford.edu/",
"http://naver.com", "http://daum.net", "http://notion.so", "https://www.kakaocorp.com/page/"] * 3
# You can specify max_worker for ProcessPoolExecutor()
with ProcessPoolExecutor(max_workers=20) as executor:
# Example 1
# Key = Future, Value = URL
start = time()
future_to_url = {executor.submit(visit, url, 5): url for url in urls}
# print(future_to_url)
for future in as_completed(future_to_url):
future.done()
print(f"{time() - start} seconds to visit {len(urls)} websites")
# Example 2
# futures = [executor.submit(count, i) for i in range(1, 15)]
#
# for future in as_completed(futures):
# print(f"{future.result()} done")
# Visit Website without pool executor
# start = time()
# tasks = []
# for url in urls:
# p = Process(target=visit, args=(url, 5))
# p.start()
# tasks.append(p)
#
# for task in tasks:
# task.join()
#
# print(f"{time() - start} seconds to visit {len(urls)} websites")
if __name__ == "__main__":
main()