Python的multiprocessing模块详解

来源:转载


通过multiprocessing模块的Process类创建一个进程的实力,并通过run()的方法来运行他

frommultiprocessingimportProcess
importos
defsub_hello(name):
print('Hello,%s!MyprocessIDis%s'%(name,os.getpid()))

if__name__=='__main__':
print('I/'mrunninginprocess%s'%os.getpid())
p=Process(target=sub_hello,args=('bobo',))
p.start()
p.join()
print('Processinend')

得到的运行结果如下:

I'm running in process 11284

Hello,bobo! My process ID is 13996

Process in end

2.通过multiprocessing模块创建进程池Pool,批量创建子进程

frommultiprocessingimportPool
importos,time,random

defmulti_task(name):
print('task%s(ID%s)isrunning'%(name,os.getpid()))
start=time.time()
time.sleep(random.random()*3)
end=time.time()
print('Imrunningin%0.2f'%(end-start))

if__name__=='__main__':
print('Processingisrunning%s'%os.getpid())
p=Pool()
foriinrange(5):
p.apply_async(multi_task,args=(i,))
p.close()
p.join()

代码运行结果如下:

Processing is running 6960

task 0(ID 12928) is running

task 1(ID 9328) is running

task 2(ID 12344) is running

task 3(ID 13412) is running

Im running in 0.57

task 4(ID 9328) is running

Im running in 1.33

Im running in 2.60

Im running in 2.59

Im running in 2.39

3,进程之间通信有两种方式,一种是Queue,一种是Pipe

frommultiprocessingimportProcess,Queue
importos
defsub_process(queue):
queue.put(['bobo','is','a','God'])
print('MyprocessIDis%s'%os.getpid())

if__name__=='__main__':
q=Queue()
p=Process(target=sub_process,args=(q,))
p.start()
p.join()
print('MyprocessIDis%sMychildrentransme%s'%(os.getpid(),q.get()))

使用pip来传递进程之间的数据

frommultiprocessingimportPipe,Process

defpip_con(pip):
pip.send(['bobo','is','a','god'])
pip.close()

if__name__=='__main__':
parent_con,child_con=Pipe()
p=Process(target=pip_con,args=(child_con,))
p.start()
p.join()
print(parent_con.recv())

进程锁,示例。两个进程同时读取一个文件的时候会发生混乱,

importmultiprocessing
importtime

defworker_with(lock,file):
withlock:
f=open(file,'a')
f.write('Lockacquiredviawith')
f.close()

defwoker_no_with(lock,file):
lock.acquire()
try:
f=open('file','a')
f.write('Lockacquireddirectly')
f.close()
finally:
lock.release()

if__name__=='__main__':
l=multiprocessing.Lock()
w=multiprocessing.Process(target=worker_with,args=(l,'c://test1.txt'))
nw=multiprocessing.Process(target=woker_no_with,args=(l,'c://test1.txt'))
w.start()
nw.start()
w.join()
nw.join()

分享给朋友:
您可能感兴趣的文章:
随机阅读: