参考资料:
https://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/0013868322563729e03f6905ea94f0195528e3647887415000
1、多任务的实现有3种方式:
多进程模式;
多线程模式;
多进程+多线程模式。
2、Python实现多进程编程,有两种方式:
(1)调用操作系统的系统调用fork,在当前进程执行过程中创建子进程。但Windows操作系统下不支持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)引用multiprocessing模块。通过该模块集成的Process类创建子进程。另外,通过os.getpid()可获取当前进程的ID。
3、通过multiprocessing模块中的Pool类可实现一次启动多个进程。实际同时执行的进程数取决于CPU内核个数。
4、Python的multiprocessing模块包装了底层的机制,提供了Queue、Pipes等多种方式来实现进程间数据交换。
下面是参考资料中给出的示例代码。
#示例代码1 Process类启动1个子进程
from multiprocessing import Process
import os
def run_proc(name):
print "running 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"
#示例代码2 进程池 from multiprocessing import Pool import os, time, random def long_time_task(name): print "Running 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() for i in range(5): p.apply_async(long_time_task, args=(i,)) print "Waiting for all subprocess done ..." p.close() p.join() print "All subprocess done"
#示例代码3 进程间通过Queue实现通信 from multiprocessing import Process, Queue import os, time, random def write(q): for value in ['A', 'B', 'C']: print 'Put %s into the queue ...' % value q.put(value) time.sleep(random.random()) def read(q): while True: value = q.get(True) print 'Get %s from the 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()
今天就学习到这里,下一节从多线程学起。