-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththreading_vs_processing.py
137 lines (101 loc) · 3.08 KB
/
threading_vs_processing.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import threading
import time
from multiprocessing import Pool
from queue import Queue
import bs4
import requests
PRIME_NUM = 1000000
PRIME_CNT = 10
REQUEST_URL = 'https://www.baidu.com/'
REQUEST_CNT = 100
def do_request(current_url):
print('.', end='', flush=True)
res = requests.get(current_url)
res.raise_for_status()
def sum_prime(num):
print('.', end='', flush=True)
sum_of_primes = 0
ix = 2
while ix <= num:
if is_prime(ix):
sum_of_primes += ix
ix += 1
return sum_of_primes
def process_cpu_queue(queue):
while True:
num = queue.get()
sum_prime(num)
queue.task_done()
def process_io_queue(queue):
while True:
current_url = queue.get()
do_request(current_url)
queue.task_done()
def is_prime(num):
if num <= 1:
return False
elif num <= 3:
return True
elif num % 2 == 0 or num % 3 == 0:
return False
i = 5
while i*i <= num:
if num % i == 0 or num % (i+2) == 0:
return False
i += 6
return True
def multi_threading_io(thread_cnt):
queue = Queue()
url_list = [REQUEST_URL] * REQUEST_CNT
for i in range(thread_cnt):
t = threading.Thread(target=process_io_queue, args=(queue,))
t.daemon = True
t.start()
start = time.time()
for current_url in url_list:
queue.put(current_url)
queue.join()
print('\n{0} threading, execute time = {1:.5f} s'.format(
thread_cnt, time.time() - start))
def multi_processing_io(process_cnt):
url_list = [REQUEST_URL] * REQUEST_CNT
start = time.time()
with Pool(process_cnt) as p:
p.map(do_request, url_list)
print('\n{0} processing, execute time = {1:.5f} s'.format(
process_cnt, time.time() - start))
def multi_threading_cpu(thread_cnt):
start = time.time()
queue = Queue()
for i in [PRIME_NUM]*PRIME_CNT:
queue.put(i)
for i in range(thread_cnt):
t = threading.Thread(target=process_cpu_queue, args=(queue,))
t.daemon = True
t.start()
queue.join()
print('\n{0} threading, execute time = {1:.5f} s'.format(
thread_cnt, time.time() - start))
def multi_processing_cpu(process_cnt):
start = time.time()
with Pool(process_cnt) as p:
p.map(sum_prime, [PRIME_NUM]*PRIME_CNT)
print('\n{0} processing, execute time = {1:.5f} s'.format(
process_cnt, time.time() - start))
if __name__ == '__main__':
print('>>> multi_threading_io, total {} requests'.format(REQUEST_CNT))
multi_threading_io(1)
multi_threading_io(2)
multi_threading_io(5)
print('\n>>> multi_processing_io, total {} requests'.format(REQUEST_CNT))
multi_processing_io(1)
multi_processing_io(2)
multi_processing_io(5)
print('\n>>> multi_threading_cpu, total {} tasks'.format(PRIME_CNT))
multi_threading_cpu(1)
multi_threading_cpu(2)
multi_threading_cpu(5)
print('\n>>> multi_processing_cpu, total {} tasks'.format(PRIME_CNT))
multi_processing_cpu(1)
multi_processing_cpu(2)
multi_processing_cpu(5)