多进程记要
# -*- coding:utf-8 -*- import multiprocessing import time def worker(interave): n=5 while n>0: print ('现在时间是{0}'.format(time.ctime())) time.sleep(interave) n-=1 if __name__=='__main__': p=multiprocessing.Process(target=worker,args=(3,)) p.start() print 'p.pid',p.pid print 'p.name',p.name print 'p.is_alive',p.is_alive()
单进程
多线程实例
import multiprocessing
import time def worker1(interave): print 'worker1' time.sleep(interave) print 'hhhhhh' def worker2(interave): print 'workery2' time.sleep(interave) print 'jjjjjj' def worker3(interave): print 'worker3' time.sleep(interave) print 'kkkkkk' if __name__=='__main__': p=multiprocessing.Process(target=worker1,args=(3,)) p2=multiprocessing.Process(target=worker2,args=(4,)) p3=multiprocessing.Process(target=worker3,args=(5,)) p.daemon=True #加上daemon当主进程结束他也跟着结束 p.start() p.join()#设置daemon结束的方法 p2.start() p3.start() print str(multiprocessing.cpu_count()) print 'p.pid',p.pid,p2.pid,p3.pid print 'p.name',p.name print 'p.is_alive',p.is_alive()