多进程和多线程编程

多任务的实现方式:

  • 多进程模式
  • 多线程模式
  • 多进程 + 多线程 模式
    python即支持多进程,又支持多线程,如下进行介绍。
多进程
操作系统fork()系统调用复制当前进程,并且在父进程和子进程中返回,父进程中返回fork出的子进程ID, 子进程中返回0;
一个父进程可以fork很多子进程;
子进程可以通过getppid()拿到父进程ID; 任何进程都可以通过getpid()拿取当前进程ID;
python的os模块封闭了常见的系统调用,其中就包括fork.
#/usr/bin/env python # _*_ encoding: utf-8 _*_import osprint '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)

运行结果:
Process (19929) start... I (19929) just created a child process (19930). I am child process (19930) and my parent is 19929.

由于Windows没有fork调用,上面的代码在Windows上无法运行。
multiprocess--跨平台的多进程支持模块 1.Process实例
#/usr/bin/env python # _*_ encoding: utf-8 _*_from multiprocessing import Process import osdef 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'

运行结果
Parent process 24556. Process will start Run child process test (24557)... Process end

2.Pool实例--进程池实现
#!/usr/bin/env python # _*_ encoding: utf-8 _*_from multiprocessing import Pool import os, time, randomdef 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() for i in range(5): p.apply_async(long_time_task, args=(i, )) print 'Waiting for all subprocesses done...' p.close()#调用close()之后就不能继续添加新的Process了 p.join() print 'All subprocesses done.'

运行结果
Parent process 27577. Waiting for all subprocesses done... Run task 0 (27579)... Run task 1 (27580)... Run task 2 (27581)... Run task 3 (27582)... Task 1 runs 0.15 seconds. Run task 4 (27580)... Task 3 runs 1.40 seconds. Task 4 runs 1.27 seconds. Task 0 runs 1.96 seconds. Task 2 runs 2.04 seconds. All subprocesses done.

请注意输出的结果,task 0,1,2,3是立刻执行的,而task 4要等待前面某个task完成后才执行,这是因为Pool的默认大小在我的电脑上是4,因此,最多同时执行4个进程。这是Pool有意设计的限制,并不是操作系统的限制。(Pool的默认大小是CPU的核数),可以根据pool的第一个参数进行修改:
p = Pool(5)
进程间通信 Python中multiprocessing模块包装了底层的机制,提供了Queue、Pipes等方式来进行进程间数据交换。
如何以Queue为示例:
#!/usr/bin/env python # _*_ encoding: utf-8 _*_from multiprocessing import Process, Queue import os, time, random# 写进程执行的代码: def write(q): for value in ['A', 'B', 'C']: print 'Put %s to queue...' % value q.put(value) time.sleep(random.random())# 读进程执行的代码: def read(q): while True: value = https://www.it610.com/article/q.get(True) print'Get %s from queue.' % valueif __name__=='__main__': # 父进程创建Queue,并传给各个子进程 q = Queue() pw = Process(target=write, args=(q,)) pr = Process(target=read, args=(q,)) pw.start() pr.start() # 等待PW结束 pw.join() # 强行结束pr进程 pr.terminate()

结果:
Put A to queue...
Get A from queue.
Put B to queue...
Get B from queue.
Put C to queue...
Get C from queue.
多线程 python标准库通过thread和threading实现多线程;thread是低级模块;threading是高级模块;
#!/usr/bin/env python # _*_ encoding: utf-8 _*_import time,threading# 新线程执行的代码 def loop(): print 'thread %s is running...' % threading.current_thread().name n = 0 while n < 5: n = n + 1 print 'thread %s >>> %s' % (threading.current_thread().name, n) time.sleep(1) print 'thread %s ended.' % threading.current_thread().nameprint 'thread %s is running...' % threading.current_thread().name t = threading.Thread(target=loop, name='LoopThread') t.start() t.join() print 'thread %s ended.' % threading.current_thread().name

结果:
thread MainThread is running...
thread LoopThread is running...
thread LoopThread >>> 1
thread LoopThread >>> 2
thread LoopThread >>> 3
thread LoopThread >>> 4
thread LoopThread >>> 5
thread LoopThread ended.
thread MainThread ended.
多线程程序需要解决的一个问题就是多线程共享变量,造成修改混乱。
需要通过threading.Lock()进行加锁解锁。
写在最后的坑:
python有一个GIL锁,Global Interpreter Lock, 使所有的python多线程只能交替执行,只能在1核心上跑。。。
在Python中,可以使用多线程,但不要指望能有效利用多核。如果一定要通过多线程利用多核,那只能通过C扩展来实现,不过这样就失去了Python简单易用的特点。
不过,也不用过于担心,Python虽然不能利用多线程实现多核任务,但可以通过多进程实现多核任务。多个Python进程有各自独立的GIL锁,互不影响。
所以Python推荐使用多进程... python支持分布式进程 【多进程和多线程编程】multiprocessing.managers子模块支持把多进程分布到多台机器上;

    推荐阅读