来源:廖雪峰
讲解看来源吧 把例子记一下
1.用fork创建进程
import os print "Process (%s) start..." % os.getpid() pid = os.fork() if pid == 0: print 'I am child process (%s) and my parent is %s' % (os.getpid(), os.getppid()) else: print 'I (%s) just created a child process (%s)' % (os.getpid(), pid)
2.用Process创建进程
import os from multiprocessing import Process def run_proc(name): print 'Run child process %s (%s)...' % (name, os.getpid()) if __name__ == '__main__': print 'Parent process %s.' % os.getpid() p = Process(target = run_proc, args=('test',)) print 'Process will start' p.start() p.join() print 'Process end'
3.进程池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(9) #指定同时跑9个进程 如果用Pool(),则同时跑cpu核数个进程 for i in range(9): p.apply_async(long_time_task, args=(i,)) print 'Waiting for all subprocesses done...' p.close() p.join() print 'All subprocesses done'
4.用Queue实现进程间通信
from multiprocessing import Process, Queue import os, time, random def write(q): for value in "ABC": print 'Put %s to queue...' % value q.put(value) time.sleep(random.random()) def read(q): 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() pr.terminate()