#!/usr/bin/-*-coding:utf-8 -*- from multiprocessing import Poolimport timedef f (x): time.sleep (1) print x return x*xif '__main__': = Pool (5) print (P.map (F,range ( ( ten) )))
frommultiprocessing Import processimport osdef info (title): Print title print'Module Name:', __name__ifHasattr (OS,'Getppid'): # only available on Unix print'Parent Process:', Os.getppid () print'Process ID:', Os.getpid () def f (name): info ('function f') Print'Hello', nameif__name__ = ='__main__': info ('Main Line') P= Process (Target=f, args= ('Bob',)) P.start () P.join ( )
#显示多进程之间不进行通信 frommultiprocessing Import processdef Run (info_title,n): Info_title.append (n) Print Info_titleinfo_title= []if__name__ = ='__main__': forIinchRangeTen): P= Process (target=run,args=(Info_title,i)) P.start () ########### #显示结果为 ############[1][0][4][6][8][7][2][9][5][3]
Python multi-process (two methods)