标签:
#!/usr/bin/env python # -*- coding:utf-8 -*- from multiprocessing import Pool import time def f(x): time.sleep(1) print x return x*x if __name__ == ‘__main__‘: p = Pool(5) print(p.map(f,range(10)))
from multiprocessing import Process import os def info(title): print title print ‘module name:‘, __name__ if hasattr(os, ‘getppid‘): # only available on Unix print ‘parent process:‘, os.getppid() print ‘process id:‘, os.getpid() def f(name): info(‘function f‘) print ‘hello‘, name if __name__ == ‘__main__‘: info(‘main line‘) p = Process(target=f, args=(‘bob‘,)) p.start() p.join()
#显示多进程之间不进行通信
#显示多进程之间不进行通信 from multiprocessing import Process def run(info_title,n): info_title.append(n) print info_title info_title = [] if __name__ == ‘__main__‘: for i in range(10): p = Process(target=run,args=(info_title,i)) p.start() ############显示结果为############ [1] [0] [4] [6] [8] [7] [2] [9] [5] [3]
标签:
原文地址:http://www.cnblogs.com/fengjian2016/p/5261246.html