-
Notifications
You must be signed in to change notification settings - Fork 303
/
cores.py
205 lines (185 loc) · 5.09 KB
/
cores.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# encoding: utf-8
__author__ = 'zhanghe'
# from multiprocessing import Pool
from multiprocessing.dummy import Pool
import gevent
import os
import time
import random
from tools import time_log
from gevent import monkey
monkey.patch_all()
def long_time_task(name):
"""
任务运行时间
:param name:
:return:
"""
print 'Run task %s (%s)...' % (name, os.getpid())
start = time.time()
# time.sleep(random.random() * 3)
time.sleep(0.3)
end = time.time()
print 'Task %s runs %0.2f seconds.' % (name, (end - start))
@time_log.time_log
def run():
print 'Parent process %s.' % os.getpid()
p = Pool()
for i in range(9):
p.apply_async(long_time_task, args=(i,))
print 'Waiting for all sub processes done...'
p.close()
p.join()
print 'All sub processes done.'
@time_log.time_log
def run_gevent():
threads = [gevent.spawn(long_time_task, i) for i in range(9)]
gevent.joinall(threads)
if __name__ == '__main__':
# run()
run_gevent()
# 这是一个多进程的例子,同时运行的进程数与机器的核心数是对应的
# 根据任务池同时运行的进程数也可以看出当前cpu的核心数,运行结果:
# 可以看出进程池子最多有4个进程
# Parent process 10903.
# Waiting for all sub processes done...
# Run task 0 (10904)...
# Run task 1 (10905)...
# Run task 2 (10906)...
# Run task 3 (10907)...
# Task 2 runs 1.56 seconds.
# Run task 4 (10906)...
# Task 4 runs 0.18 seconds.
# Run task 5 (10906)...
# Task 3 runs 2.04 seconds.
# Run task 6 (10907)...
# Task 6 runs 0.05 seconds.
# Run task 7 (10907)...
# Task 1 runs 2.53 seconds.
# Run task 8 (10905)...
# Task 0 runs 2.62 seconds.
# Task 5 runs 1.52 seconds.
# Task 8 runs 1.68 seconds.
# Task 7 runs 2.77 seconds.
# All sub processes done.
# 查看系统CPU参数,四核心
# zhanghe@ubuntu:~$ cat /proc/cpuinfo | grep cpu
# cpu family : 6
# cpu MHz : 800.000
# cpu cores : 4
# cpuid level : 13
# cpu family : 6
# cpu MHz : 800.000
# cpu cores : 4
# cpuid level : 13
# cpu family : 6
# cpu MHz : 3101.000
# cpu cores : 4
# cpuid level : 13
# cpu family : 6
# cpu MHz : 800.000
# cpu cores : 4
# cpuid level : 13
# zhanghe@ubuntu:~$
# from multiprocessing import Pool
# time.sleep(0.3)
# 方法run开始时间:Thu Apr 30 00:48:04 2015
# Parent process 17113.
# Waiting for all sub processes done...
# Run task 1 (17115)...
# Run task 0 (17114)...
# Task 1 runs 0.30 seconds.
# Run task 2 (17115)...
# Task 0 runs 0.30 seconds.
# Run task 3 (17114)...
# Task 2 runs 0.30 seconds.
# Run task 4 (17115)...
# Task 3 runs 0.30 seconds.
# Run task 5 (17114)...
# Task 4 runs 0.30 seconds.
# Run task 6 (17115)...
# Task 5 runs 0.30 seconds.
# Run task 7 (17114)...
# Task 6 runs 0.30 seconds.
# Run task 8 (17115)...
# Task 7 runs 0.30 seconds.
# Task 8 runs 0.30 seconds.
# All sub processes done.
# 方法run结束时间:Thu Apr 30 00:48:05 2015
# 方法run运行时间:1.72S
# from multiprocessing.dummy import Pool
# time.sleep(0.3)
# 方法run开始时间:Thu Apr 30 00:51:27 2015
# Parent process 17244.
# Waiting for all sub processes done...
# Run task 0 (17244)...
# Run task 1 (17244)...
# Task 0 runs 0.30 seconds.
# Task 1 runs 0.30 seconds.Run task 2 (17244)...
#
# Run task 3 (17244)...
# Task 2 runs 0.30 seconds.Task 3 runs 0.30 seconds.
# Run task 4 (17244)...
#
# Run task 5 (17244)...
# Task 4 runs 0.30 seconds.
# Task 5 runs 0.30 seconds.Run task 6 (17244)...
#
# Run task 7 (17244)...
# Task 6 runs 0.30 seconds.
# Run task 8 (17244)...
# Task 7 runs 0.30 seconds.
# Task 8 runs 0.30 seconds.
# All sub processes done.
# 方法run结束时间:Thu Apr 30 00:51:29 2015
# 方法run运行时间:1.54S
# import gevent
# time.sleep(0.3)
# 方法run_gevent开始时间:Thu Apr 30 01:13:52 2015
# Run task 0 (17964)...
# Task 0 runs 0.30 seconds.
# Run task 1 (17964)...
# Task 1 runs 0.30 seconds.
# Run task 2 (17964)...
# Task 2 runs 0.30 seconds.
# Run task 3 (17964)...
# Task 3 runs 0.30 seconds.
# Run task 4 (17964)...
# Task 4 runs 0.30 seconds.
# Run task 5 (17964)...
# Task 5 runs 0.30 seconds.
# Run task 6 (17964)...
# Task 6 runs 0.30 seconds.
# Run task 7 (17964)...
# Task 7 runs 0.30 seconds.
# Run task 8 (17964)...
# Task 8 runs 0.30 seconds.
# 方法run_gevent结束时间:Thu Apr 30 01:13:55 2015
# 方法run_gevent运行时间:2.74S
# import gevent
# time.sleep(0.3)
# from gevent import monkey
# monkey.patch_all()
# 方法run_gevent开始时间:Thu Apr 30 01:17:05 2015
# Run task 0 (18038)...
# Run task 1 (18038)...
# Run task 2 (18038)...
# Run task 3 (18038)...
# Run task 4 (18038)...
# Run task 5 (18038)...
# Run task 6 (18038)...
# Run task 7 (18038)...
# Run task 8 (18038)...
# Task 0 runs 0.30 seconds.
# Task 1 runs 0.30 seconds.
# Task 2 runs 0.30 seconds.
# Task 3 runs 0.30 seconds.
# Task 4 runs 0.30 seconds.
# Task 5 runs 0.30 seconds.
# Task 6 runs 0.30 seconds.
# Task 7 runs 0.30 seconds.
# Task 8 runs 0.30 seconds.
# 方法run_gevent结束时间:Thu Apr 30 01:17:05 2015
# 方法run_gevent运行时间:0.32S
# 结果可以看出使用multiprocessing.dummy模块比multiprocessing速度略有提升
# 使用打补丁后的gevent,效率最快