#!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 ()
Multi-process Queue
#!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 () #进程队列, which allows the child process to access the data of the master process
p = Process (target=f,args= (q,))
P.start ()
Print (Q.get ())
P.join ()
Multi-Process Pipeline
#!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 __nam e__ = = "__main__":
data = "Hello"
Parent_conn,child_conn = Pipe () #接收管道的两个对象
P = Process (Target=fun,ar Gs= (Child_conn,data,))
P.start ()
Print (PARENT_CONN.RECV ())
Print (PARENT_CONN.RECV ())
Parent_co Nn.send ("word")
P.join ()
Multi-process 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)) #生成一个列表, the default value is 0,1,2,3,4
Pro_obj = [] #用于存储子进程
for I in range: #生成 10 child processes
P = Process (target=f,args= (d,l))
P.start ()
Pro_obj.append (p)
For pro in Pro_obj:
Pro.join ()
Print (d)
Print (L)
Process Lock
#!usr/bin/env python
#-*-coding:utf-8-*-
__author__ = "Samson"
from multiprocessing Import Lock,process
#进程锁可以防止一个进程在打印数据时, another process is jumping in. Print Data
def f (l,d):
L.acquire ()
Print ("Hello%s"%d)
L.re Lease ()
If __name__ = = "__main__":
Lock = Lock ()
for I in range:
p = Process (Target=f,args = (lock,i)) #注意, you have to pass the lock in.
P.start ()
Process Pool
#!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 ("Main process:", 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 (), note that the rollback function is called by the main process
Print ("End")
Pool.close () #关闭进程池
Pool.join () #进程池中进程执行完毕后再关闭, if the comment, then the program closes directly; Note that the method must be placed after the Pool.close () function
Python: Multi-process, multi-process queue, multi-process pipeline, Manager, process lock, process pool