数据工程师我的Python自学之路生活不易 我用python

multiprocessing模块

2017-03-11  本文已影响76人  苟雨

multiprocessing的用法与threading的用法十分相似

from multiprocessing import Process 
def f(): 
  print('multiprocessing') 
  p = Process(target=f,args=()) 
  p.start() 
  p.join()

在两个进程中交换对象
使用队列

from multiprocessing import Process,Queue,Pipe
def use_queue(q):
    q.put([42,'hello'])
q = Queue()
p = Process(target=use_queue,args=(q,))
p.start()
print(q.get())
p.join()

[42, 'hello']

使用Pipe

def use_pipe(conn):
    conn.send([42,'hello'])
    conn.close()
parent_conn,child_conn = Pipe()
p_p = Process(target=use_pipe,args=(child_conn,))
p_p.start()
print(parent_conn.recv())
p_p.join()
    
[42, 'hello']

两个进程异步执行

from multiprocessing import Process,Lock
def syn_f(l,num):
    l.acquire()
    print('hello synchronization',num)
    l.release()
lock = Lock()
for num in range(10):
    p_s = Process(target=syn_f,args=(lock,num)).start()
    
('hello synchronization', 0)
('hello synchronization', 1)
('hello synchronization', 2)
('hello synchronization', 3)
('hello synchronization', 4)
('hello synchronization', 6)
('hello synchronization', 5)
('hello synchronization', 7)
('hello synchronization', 8)
('hello synchronization', 9)

使用进程池

from multiprocessing import Pool
def f(x):
    return x*x
po = Pool(5)
result = po.apply_async(f,[10])
result.get(timeout=1)
# 使用map函数来迭代应用
print(po.map(f,range(10)))
​
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

在两个进程之间分享状态

from multiprocessing import Process,Array,Value
def share_state(n,a):
    n.value = 1234
    for i in range(len(a)):
        a[i] = -a[i]
    
num = Value('d',1) # d表示双精度整数
arr = Array('i',range(10)) # i表示无符号整数
p = Process(target=share_state,args=(num,arr))
p.start()
p.join()
​
print(num.value)
print(arr[:])
​
1234.0
[0, -1, -2, -3, -4, -5, -6, -7, -8, -9]

使用manager对象管理共享

from multiprocessing import Process,Manager
def use_manager(d,l):
    d[1] = 1
    d['o'] = 'o'
    l.reverse()
manager = Manager()
d = manager.dict()
l = manager.list(range(10))
p = Process(target=use_manager,args=(d,l))
p.start()
p.join()
​
print(d)
print(l)
{1: 1, 'o': 'o'}
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
上一篇 下一篇

猜你喜欢

热点阅读