#!usr/bin/env python
# -*- coding:utf-8 -*-
__author__ = "Samson"
import multiprocessing
import time
def run(name):
time.sleep(2)
print("process start...%s" % name)
if __name__ == "__main__":
for i in range(10):
p = multiprocessing.Process(target=run,args=("bob",))
p.start()
多进程队列
#!usr/bin/env python
# -*- coding:utf-8 -*-
__author__ = "Samson"
from multiprocessing import Process,Queue
def f(q):
q.put([1,"ss",None])
if __name__ == "__main__":
q = Queue()#进程队列,通过它可以使得子进程访问主进程的数据
p = Process(target=f,args=(q,))
p.start()
print(q.get())
p.join()
多进程管道
#!usr/bin/env python
# -*- coding:utf-8 -*-
__author__ = "Samson"
from multiprocessing import Pipe,Process
def fun(conn,data):
conn.send(data)
conn.send(data)
print(conn.recv())
if __name__ == "__main__":
data = "hello"
parent_conn,child_conn = Pipe()#接收管道的两个对象
p = Process(target=fun,args=(child_conn,data,))
p.start()
print(parent_conn.recv())
print(parent_conn.recv())
parent_conn.send("word")
p.join()
多进程manager
#!usr/bin/env python
# -*- coding:utf-8 -*-
__author__ = "Samson"
from multiprocessing import Process,Manager
import os
def f(d, l):
d[os.getpid()] =os.getppid()#存储相应进程的pid
l.append(os.getpid())
print(l)
if __name__ == "__main__":
with Manager() as manager:
d = manager.dict()#生成一个字典
l = manager.list(range(5))#生成一个列表,默认值为0,1,2,3,4
pro_obj = []#用于存储子进程
for i in range(10):#生成十个子进程
p = Process(target=f,args=(d,l))
p.start()
pro_obj.append(p)
for pro in pro_obj:
pro.join()
print(d)
print(l)
进程锁
#!usr/bin/env python
# -*- coding:utf-8 -*-
__author__ = "Samson"
from multiprocessing import Lock,Process
#进程锁可以防止一个进程在打印数据时,另一个进程插队进来打印数据
def f(l,d):
l.acquire()
print("hello %s" %d)
l.release()
if __name__ == "__main__":
lock = Lock()
for i in range(10):
p = Process(target=f,args=(lock,i))#注意,得把锁也传进去
p.start()
进程池
#!usr/bin/env python
# -*- coding:utf-8 -*-
__author__ = "Samson"
from multiprocessing import Pool,Process
import time,os
def Foo(i):
time.sleep(2)
print("in process",os.getpid())
return i + 100
def Bar(arg):
print("--exec done:",arg,os.getpid())
if __name__ == "__main__":
pool = Pool(processes=5)#允许进程池同时放入5个进程
print("主进程:",os.getpid())
for i in range(10):
# pool.apply(func=Foo,args=(i,))#串行
# pool.apply_async(func=Foo,args=(i,))#并行
pool.apply_async(func=Foo,args=(i,),callback=Bar)#并行同时调用回掉函数Bar(),注意该回掉函数是由主进程调用的
print("end")
pool.close()#关闭进程池
pool.join()#进程池中进程执行完毕后再关闭,如果注释,那么程序直接关闭;注意,该方法必须放在pool.close()函数后面