多进程multiprocessing

1、多进程Process

调用方法:

from multiprocessing import  Process,freeze_support
import time
def f(i):
    time.sleep(1)
    print(i)


# 这里如果不写if __name__ == "__main__":的话会报freeze_support()错误
if __name__ == "__main__":
    for i in range(10):
        p = Process(target=f,args=(i,))
        p.start()
    print("end")

 

2、队列Queue

Queue为先进先出,get()一次,Queue里的那个被get的值会被拿走,当队列为空时get()方法会阻塞,

调用方法:

from multiprocessing import Process,Queue
import time


def f(i,q):
    time.sleep(1)
    q.put(i)


if __name__ == "__main__":
    q = Queue()
    for i in range(2):
        p = Process(target=f,args=(i,q,))
        p.start()

    for i in range(2):
        print(q.get())

2.1 Queue中的数据是可以在子进程间共享的。

from multiprocessing import Process,Queue
import random
import os

def f(q):
    if q.qsize() != 0:
        print("pid:%s get %s from queue"%(os.getpid(),q.get()))
    else:
        n = random.randrange(1,10)
        print("pid:%s put %s into the queue"%(os.getpid(),n))
        q.put(n)


if __name__ == "__main__":
    print("main pid is:%s"%os.getpid())
    q = Queue()
    p = Process(target=f,args=(q,))
    p.start()

    p1 = Process(target=f,args=(q,))
    p1.start()

第一个进程放入数据,第2个进程可以取到,运行结果如下:

main pid is:7300
pid:5208 put 2 into the queue
pid:6104 get 2 from queue

  

3、Pipe

示例:
from multiprocessing import Process,Queue,Pipe
import time

def f(i,conn):
    conn.send(i*10)

if __name__ == "__main__":
    parent, child = Pipe()
    for i in range(2):
        p = Process(target=f,args=(i,child))
        p.start()
    for i in range(2):
        print(parent.recv())

4、Manager,进程间数据共享

如果有子进程在使用Manage()对象时,在父进程不能使用这个对象,所以要等所有子进程结束即需使用p.join()后方可在父进程使用Manage()的对象。

如果这里注释join()会报:FileNotFoundError: [WinError 2] 系统找不到指定的文件。

from multiprocessing import Process,Manager
import time

def f(d,l,i):
    time.sleep(5-i)   # 最后起的进程睡眠最少时间,所以父进程不能简单的使用p.join(),而是要把p加入p_list中,然后循环p_list,并使用join()方法
    d[i] = i*10
    l.append(i*100)


if __name__ == "__main__":
    with Manager() as m:
        d = m.dict()
        l = m.list([1,2,3])
        p_list = []
        for i in range(5):
            p = Process(target=f,args=(d,l,i,))
            p.start()
            p_list.append(p)

        for p in p_list:
            p.join()
     
        print("d:",d)
        print("l:",l)

5、进程池Pool

Pool满时后面的进程会被挂起,当pool中一个进程结束时,挂起的进程会马上开始执行。callback回调,将target的函数的teturn的值返回给callback函数的参数。

from multiprocessing import Process,Pool
import time

def f(i):
    print("begin:",i,time.time())
    time.sleep(i)
    print("end:",i,time.time())
    return i

def bar(i):
    if i == 9:
        print("bar:",i)

if __name__ == "__main__":
    pool = Pool(5)
    for i in range(1,10):
        pool.apply_async(func=f,args=(i,),callback=bar)
    pool.close()
    pool.join()

#执行结果:
begin: 1 1466267934.986106
begin: 2 1466267934.992107
begin: 3 1466267935.0080783
begin: 4 1466267935.0166094
begin: 5 1466267935.0236104
end: 1 1466267935.9862905
begin: 6 1466267935.9864767
end: 2 1466267936.992654
begin: 7 1466267936.9931495
end: 3 1466267938.0089972
begin: 8 1466267938.0089972
end: 4 1466267939.016981
begin: 9 1466267939.016981
end: 5 1466267940.0240061
end: 6 1466267941.9866436
end: 7 1466267943.9936304
end: 8 1466267946.0098124
end: 9 1466267948.0178423
bar: 9

 

posted @ 2016-06-18 21:54  电神  阅读(544)  评论(0编辑  收藏  举报