进程各自持有一份数据,默认无法共享数据
#!/usr/bin/env python
#coding:utf-8
from multiprocessing import Process
from multiprocessing import Manager
import time
li = []
def foo(i):
li.append(i)
print ‘say hi‘,li
for i in range(10):
p = Process(target=foo,args=(i,))
p.start()
print ‘ending‘,li
为了解决进程之间的数据共享问题,可以使用如下方法:
#方法一,Array
from
multiprocessing
import
Process,Array
temp
=
Array(
‘i‘
, [
11
,
22
,
33
,
44
])
def
Foo(i):
temp[i]
=
100
+
i
for
item
in
temp:
print
i,
‘----->‘
,item
for
i
in
range
(
2
):
p
=
Process(target
=
Foo,args
=
(i,))
p.start()
#方法二:manage.dict()共享数据
from
multiprocessing
import
Process,Manager
manage
=
Manager()
dic
=
manage.
dict
()
def
Foo(i):
dic[i]
=
100
+
i
print
dic.values()
for
i
in
range
(
2
):
p
=
Process(target
=
Foo,args
=
(i,))
p.start()
p.join()
类型对应表:
‘c‘: ctypes.c_char, ‘u‘: ctypes.c_wchar,
‘b‘: ctypes.c_byte, ‘B‘: ctypes.c_ubyte,
‘h‘: ctypes.c_short, ‘H‘: ctypes.c_ushort,
‘i‘: ctypes.c_int, ‘I‘: ctypes.c_uint,
‘l‘: ctypes.c_long, ‘L‘: ctypes.c_ulong,
‘f‘: ctypes.c_float, ‘d‘: ctypes.c_double
当创建进程时(非使用时),共享数据会被拿到子进程中,当进程中执行完毕后,再赋值给原值。
进程锁实例
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from multiprocessing import Process, Array, RLock
def Foo(lock,temp,i):
"""
将第0个数加100
"""
lock.acquire()
temp[0] = 100+i
for item in temp:
print i,‘----->‘,item
lock.release()
lock = RLock()
temp = Array(‘i‘, [11, 22, 33, 44])
for i in range(20):
p = Process(target=Foo,args=(lock,temp,i,))
p.start()
进程池
进程池内部维护一个进程序列,当使用时,则去进程池中获取一个进程,如果进程池序列中没有可供使用的进进程,那么程序就会等待,直到进程池中有可用进程为止。
进程池中有两个方法:
apply
apply_async
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from
multiprocessing
import
Process,Pool
import
time
def
Foo(i):
time.sleep(
2
)
return
i
+
100
def
Bar(arg):
print
arg
pool
=
Pool(
5
)
#print pool.apply(Foo,(1,))
#print pool.apply_async(func =Foo, args=(1,)).get()
for
i
in
range
(
10
):
pool.apply_async(func
=
Foo, args
=
(i,),callback
=
Bar)
print
‘end‘
pool.close()
pool.join()
#进程池中进程执行完毕后再关闭,如果注释,那么程序直接关闭。
本文出自 “平平淡淡才是真” 博客,请务必保留此出处http://ucode.blog.51cto.com/10837891/1766259
原文地址:http://ucode.blog.51cto.com/10837891/1766259