本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理
本篇文章来自腾讯云 作者:孤独的明月
( 想要学习Python?Python学习交流群:1039649593,满足你的需求,资料都已经上传群文件流,可以自行下载!还有海量最新2020python学习资料。 )
multiprocessing模块就是跨平台版本的多进程模块,提供了一个Process类来代表一个进程对象,这个对象可以理解为是一个独立的进程,可以执行另外的事情
# -*- coding:utf-8 -*- from multiprocessing import Process import time def run_proc(): """子进程要执行的代码""" while True: print("----2----") time.sleep(1) if __name__=='__main__': p = Process(target=run_proc) p.start() while True: print("----1----") time.sleep(1)
创建子进程时,只需要传入一个执行函数和函数的参数,创建一个Process实例,用start()方法启动
# -*- coding:utf-8 -*- from multiprocessing import Process import os import time def run_proc(): """子进程要执行的代码""" print('子进程运行中,pid=%d...' % os.getpid()) # os.getpid获取当前进程的进程号 print('子进程将要结束...') if __name__ == '__main__': print('父进程pid: %d' % os.getpid()) # os.getpid获取当前进程的进程号 p = Process(target=run_proc) p.start()
# -*- coding:utf-8 -*- from multiprocessing import Process import os from time import sleep def run_proc(name, age, **kwargs): for i in range(10): print('子进程运行中,name= %s,age=%d ,pid=%d...' % (name, age, os.getpid())) print(kwargs) sleep(0.2) if __name__=='__main__': p = Process(target=run_proc, args=('test',18), kwargs={"m":20}) p.start() sleep(1) # 1秒中之后,立即结束子进程 p.terminate() p.join() 运行结果: 子进程运行中,name= test,age=18 ,pid=45097... {'m': 20} 子进程运行中,name= test,age=18 ,pid=45097... {'m': 20} 子进程运行中,name= test,age=18 ,pid=45097... {'m': 20} 子进程运行中,name= test,age=18 ,pid=45097... {'m': 20} 子进程运行中,name= test,age=18 ,pid=45097... {'m': 20}
# -*- coding:utf-8 -*- from multiprocessing import Process import os import time nums = [11, 22] def work1(): """子进程要执行的代码""" print("in process1 pid=%d ,nums=%s" % (os.getpid(), nums)) for i in range(3): nums.append(i) time.sleep(1) print("in process1 pid=%d ,nums=%s" % (os.getpid(), nums)) def work2(): """子进程要执行的代码""" print("in process2 pid=%d ,nums=%s" % (os.getpid(), nums)) if __name__ == '__main__': p1 = Process(target=work1) p1.start() p1.join() p2 = Process(target=work2) p2.start() 运行结果: in process1 pid=11349 ,nums=[11, 22] in process1 pid=11349 ,nums=[11, 22, 0] in process1 pid=11349 ,nums=[11, 22, 0, 1] in process1 pid=11349 ,nums=[11, 22, 0, 1, 2] in process2 pid=11350 ,nums=[11, 22]
可以使用multiprocessing模块的Queue实现多进程之间的数据传递,Queue本身是一个消息列队程序。
我们以Queue为例,在父进程中创建两个子进程,一个往Queue里写数据,一个从Queue里读数据:
from multiprocessing import Process, Queue import os, time, random # 写数据进程执行的代码: def write(q): for value in ['A', 'B', 'C']: print('Put %s to queue...' % value) q.put(value) time.sleep(random.random()) # 读数据进程执行的代码: def read(q): while True: if not q.empty(): value = q.get(True) print('Get %s from queue.' % value) time.sleep(random.random()) else: break if __name__=='__main__': # 父进程创建Queue,并传给各个子进程: q = Queue() pw = Process(target=write, args=(q,)) pr = Process(target=read, args=(q,)) # 启动子进程pw,写入: pw.start() # 等待pw结束: pw.join() # 启动子进程pr,读取: pr.start() pr.join() # pr进程里是死循环,无法等待其结束,只能强行终止: print('') print('所有数据都写入并且读完') """ 输入如下: Put A to queue... Put B to queue... Put C to queue... Get A from queue. Get B from queue. Get C from queue. 所有数据都写入并且读完 """