通过multiprocessing模块的Process类创建一个进程的实力,并通过run()的方法来运行他
from multiprocessing import Processimport osdef sub_hello(name): print('Hello,%s! My process ID is %s' %(name,os.getpid()))if __name__ == '__main__': print('I\'m running in process %s' %os.getpid()) p = Process(target=sub_hello, args=('bobo',)) p.start() p.join() print('Process in end')
得到的运行结果如下:
I'm running in process 11284
Hello,bobo! My process ID is 13996
Process in end
2.通过multiprocessing模块创建进程池Pool,批量创建子进程
from multiprocessing import Poolimport os,time,randomdef multi_task(name): print('task %s(ID %s) is running'%(name,os.getpid())) start=time.time() time.sleep(random.random()*3) end=time.time() print('Im running in %0.2f'%(end-start))if __name__=='__main__': print('Processing is running %s'%os.getpid()) p=Pool() for i in range(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
from multiprocessing import Process,Queueimport osdef sub_process(queue): queue.put(['bobo','is','a','God']) print('My process ID is %s'%os.getpid())if __name__=='__main__': q=Queue() p=Process(target=sub_process,args=(q,)) p.start() p.join() print('Myprocess ID is %s Mychildren trans me %s'%(os.getpid(),q.get()))
使用pip来传递进程之间的数据
from multiprocessing import Pipe,Processdef pip_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())
进程锁,示例。两个进程同时读取一个文件的时候会发生混乱,
import multiprocessingimport timedef worker_with(lock,file): with lock: f=open(file,'a') f.write('Lock acquired via with') f.close()def woker_no_with(lock,file): lock.acquire() try: f=open('file','a') f.write('Lock acquired directly') 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()