1-多进程(multiprocessing), 1个父进程可以有多少子进程
1.1下面的例子演示了启动一个子进程并等待其结束
from multiprocessing import Process import os def run_proc(name): print('Run child process %s (%s)' % (name,os.getpid())) #父进程的ID if __name__ =='__main__': print('Parent process %s. ' % os.getpid()) p = Process(target=run_proc, args=('test', )) print('Child process will start') p.start() #开始进程 p.join() #待子子进程结束 print('Child process end')
1.2 pool
如果要启动大量的子进程,可以用进程池的方式批量创建子进程:
from multiprocessing import Pool import os, time, random def long_time_task(name): print("Run task %s (%s)" % (name, os.getpid())) start = time.time() time.sleep(random.random()*3) end = time.time() print("Task %s runs %0.2f seconds" % (name, (end - start))) if __name__ =='__main__': print('Parent process %s' % os.getpid()) p = Pool(4) for i in range(5): p.apply_async(long_time_task, args=(i,)) print("Waiting for all subprocesses done...") p.close() p.join(); #调用join()之前必须先调用close(),调用close()之后就不能继续添加新的Process了。 print("All subprocesses done.")
1.3子进程
import subprocess print("$ nslookup www.python.org") r = subprocess.call(['nslookup','www.python.org']) print("exit code:", r)
1.4进程间通讯
Python的multiprocessing模块包装了底层的机制,提供了Queue、Pipes等多种方式来交换数据。
from multiprocessing import Process,Queue import os, time, random def write(q): print('Process to write:%s' % os.getpid()) for value in ['A','B','C']: print('Put %s to queue...' % value) q.put(value) #写入数据 time.sleep(random.random()) def read(q): print('Process to read: %s' % os.getpid()) while True: value = q.get(True) #等待写入 print('Get %s from queue.' % value) if __name__ == '__main__': q = Queue() pw = Process(target=write, args=(q,)) pr = Process(target=read, args=(q,)) pw.start() #开始写 pr.start() #开始读 pw.join() #等等pw结束 pr.terminate() #由于是死循环,强制终止程序