1
2
3
4
5
6
7
|
1
.多进程的使用方法
2
.进程间的通信之multiprocessing.Manager()使用
3
.Python进程池
(
1
)比较简单的例子
(
2
)多个进程多次并发的情况
(
3
)验证apply.async方法是非阻塞的
(
4
)验证apply.async中的
get
()方法是阻塞的
|
1
2
3
4
5
6
7
8
9
10
|
from multiprocessing
import
Process #从多进程模块中导入Process
import
time
def sayHi(name):
print
'Hi my name is %s'
% name
time.sleep(
3
)
for
i
in
range(
10
):
p = Process(target=sayHi, args=(i,)) #调用多进程使用方法
p.start() #开始执行多进程
|
1
2
3
4
5
6
7
8
9
10
11
|
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day6$ python multiprocssing8.py
Hi my name
is
2
Hi my name
is
3
Hi my name
is
6
Hi my name
is
1
Hi my name
is
4
Hi my name
is
5
Hi my name
is
0
Hi my name
is
7
Hi my name
is
8
Hi my name
is
9
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
xpleaf@xpleaf-machine:~$ ps -ef | grep mul*
xpleaf
10468
1827
1
19
:
34
pts/
1
00
:
00
:
00
python multiprocssing8.py
xpleaf
10469
10468
0
19
:
34
pts/
1
00
:
00
:
00
python multiprocssing8.py
xpleaf
10470
10468
0
19
:
34
pts/
1
00
:
00
:
00
python multiprocssing8.py
xpleaf
10471
10468
0
19
:
34
pts/
1
00
:
00
:
00
python multiprocssing8.py
xpleaf
10472
10468
0
19
:
34
pts/
1
00
:
00
:
00
python multiprocssing8.py
xpleaf
10473
10468
0
19
:
34
pts/
1
00
:
00
:
00
python multiprocssing8.py
xpleaf
10474
10468
0
19
:
34
pts/
1
00
:
00
:
00
python multiprocssing8.py
xpleaf
10475
10468
0
19
:
34
pts/
1
00
:
00
:
00
python multiprocssing8.py
xpleaf
10476
10468
0
19
:
34
pts/
1
00
:
00
:
00
python multiprocssing8.py
xpleaf
10477
10468
0
19
:
34
pts/
1
00
:
00
:
00
python multiprocssing8.py
xpleaf
10478
10468
0
19
:
34
pts/
1
00
:
00
:
00
python multiprocssing8.py
xpleaf
10480
8436
0
19
:
34
pts/
2
00
:
00
:
00
grep --color=auto mul*
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
import
multiprocessing
import
time
def worker(d, key, value):
d[key] = value
mgr = multiprocessing.Manager()
d = mgr.dict()
jobs = [] #用来接收多进程函数的返回的结果,存放的是函数的入口
for
i
in
range(
10
):
jobs.append(multiprocessing.Process(target=worker,args=(d,i,i*i)))
for
j
in
jobs: #执行存放的函数入口
j.start()
for
j
in
jobs: #检测进程是否执行完毕
j.join()
#time.sleep(
1
) #如果有join()来进程进程是否执行完毕,则这里可以省略
print (
'Results:'
)
print d
|
1
2
3
|
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day6$ python multiprocssing_manager9.py
Results:
{
0
:
0
,
1
:
1
,
2
:
4
,
3
:
9
,
4
:
16
,
5
:
25
,
6
:
36
,
7
:
49
,
8
:
64
,
9
:
81
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
from multiprocessing
import
Process,Pool #导入Pool模块
import
time
def sayHi(num):
time.sleep(
1
)
return
num*num
p = Pool(processes=
5
) #定义进程池的数量为
5
result = p.apply_async(sayHi, [
10
]) #开始执行多进程,async为异步执行,即不会等待其它
#子进程的执行结果,为非阻塞模式,除非使用了
get
()方法,
get
()方法会等待子进程返回执行结果,
#再去执行下一次进程,可以看后面的例子;同理下有apply方法,阻塞模式,会等待子进程返回执行结果
print result.
get
() #
get
()方法
|
1
2
3
4
5
6
|
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day6$ time python multiprocssing_pool10.py
100
real 0m1.066s
user 0m0.016s
sys 0m0.032s
|
1
2
3
4
5
6
7
8
9
10
11
12
|
from multiprocessing
import
Process,Pool
import
time
def sayHi(num):
time.sleep(
1
)
return
num*num
p = Pool(processes=
5
)
result = p.map(sayHi,range(
3
))
for
i
in
result:print i
|
1
2
3
4
|
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day6$ python multiprocssing_pool10.py
0
1
4
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
from multiprocessing
import
Process,Pool
import
time
def sayHi(num):
time.sleep(
1
)
return
num*num
p = Pool(processes=
5
)
result_list = []
for
i
in
range(
30
):
result_list.append(p.apply_async(sayHi, [i]))
for
res
in
result_list:
print res.
get
()
|
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
|
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day6$ python multiprocssing_pool_2_11.py
0
1
4
9
16
25
36
49
64
81
100
121
144
169
196
225
256
289
324
361
400
441
484
529
576
625
676
729
784
841
|
1
2
3
4
5
|
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day6$ time python multiprocssing_pool_2_11.py | grep real
real 0m6.143s
user 0m0.052s
sys 0m0.028s
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
from multiprocessing
import
Process,Pool
import
time
def sayHi(num):
time.sleep(
1
)
return
num*num
p = Pool(processes=
100
)
result_list = []
for
i
in
range(
600
):
result_list.append(p.apply_async(sayHi, [i]))
for
res
in
result_list:
print res.
get
()
|
1
2
3
4
5
|
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day6$ time python multiprocssing_pool_2_11.py | grep real
real 0m6.371s
user 0m0.080s
sys 0m0.128s
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
from multiprocessing
import
Process,Pool
import
time
def sayHi(num):
time.sleep(
10
)
return
num*num
p = Pool(processes=
5
)
result_list = []
for
i
in
range(
30
):
result_list.append(p.apply_async(sayHi, [i]))
for
res
in
result_list:
print res.
get
()
|
1
2
3
4
5
|
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day6$ time python multiprocssing_pool_2_11.py | grep real
real 0m0.149s
user 0m0.020s
sys 0m0.024s
|
1
2
|
xpleaf@xpleaf-machine:~$ ps -ef | grep mul*
xpleaf
11499
8436
0
20
:
35
pts/
2
00
:
00
:
00
grep --color=auto mul*
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
from multiprocessing
import
Process,Pool
import
time
def sayHi(num):
time.sleep(
3
)
return
num*num
p = Pool(processes=
5
)
result_list = []
for
i
in
range(
30
):
result_list.append(p.apply_async(sayHi, [i]))
time.sleep(
3
)
|
1
2
3
4
5
|
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day6$ time python multiprocssing_pool_2_11.py | grep real
real 0m3.107s
user 0m0.040s
sys 0m0.032s
|
1
2
3
4
5
6
7
8
|
xpleaf@xpleaf-machine:~$ ps -ef | grep mul*
xpleaf
11515
1827
4
20
:
39
pts/
1
00
:
00
:
00
python multiprocssing_pool_2_11.py
xpleaf
11517
11515
0
20
:
39
pts/
1
00
:
00
:
00
python multiprocssing_pool_2_11.py
xpleaf
11518
11515
0
20
:
39
pts/
1
00
:
00
:
00
python multiprocssing_pool_2_11.py
xpleaf
11519
11515
0
20
:
39
pts/
1
00
:
00
:
00
python multiprocssing_pool_2_11.py
xpleaf
11520
11515
0
20
:
39
pts/
1
00
:
00
:
00
python multiprocssing_pool_2_11.py
xpleaf
11521
11515
0
20
:
39
pts/
1
00
:
00
:
00
python multiprocssing_pool_2_11.py
xpleaf
11526
8436
0
20
:
|