python3学习

Python3(11) Python 进程和线程

2018-02-01  本文已影响354人  猿来如痴

本系列主要学习Python的基本使用和语法知识,后续可能会围绕着AI学习展开。
Python3 (1) Python语言的简介
Python3 (2) Python语法基础
Python3 (3) Python函数
Python3 (4) Python高级特性
Python3 (5) Python 函数式编程
Python3 (6) Python 模块
Python3 (7) Python 面向对象编程
Python3 (8) Python 面向对象高级编程
Python3 (9) Python 错误、调试和测试
Python3 (10) Python IO编程
Python3 (11) Python 进程和线程
进程和线程是多并发开发中非常重要的两个概念,也是衡量一个开发人员技术水平的一个很重要依据,可想而知,应用好进程和线程的难度有多大,不是一天或者一篇文章可以学到的,而是一个开发人员慢慢成长,在项目中积累,根据各种应用场景,选择最佳的技术方案。所以我们今天只聊一些进程、线程的概念,和Python中封装的一些使用方法。千里之行,始于足下,我们开始吧。

进程和线程

进程和线程是多任务操作系统中的概念 ,如Mac OS X,UNIX,Linux,Windows等操作系统,对于操作系统来说,一个任务就是一个进程(Process),如在一台Android设备(android 采用Linux做内核)上打开一个网易云客户端听歌、打开一个微信客户端聊天、打开一个今日头条看新闻等每一个应用就是一个进程,操作系统会轮流的将多任务调度到核心的CPU上执行。现在的硬件CPU基本上都是多核,处理能力成倍的提升。 线程就更好理解了,因为线程是最小的执行单元,所以每个进程至少拥有一个线程,比如android的某个应用打开时就创建了一个主线程,如果要进行IO操作、网络请求等耗时操作就需要开启多个工作线程,这就是在一个进程中同时创建多个子任务(Thread) 的典型例子。Python既支持多进程,又支持多线程,我们会讨论如何编写这两种多任务程序。

多进程

Python是跨平台的,提供了一个跨平台的多进程支持。multiprocessing模块就是跨平台版本的多进程模块。但是针对Unix/Linux操作系统提供了一个fork(),所以这两种操作系统或延伸的系统如mac(基于BSD(Unix的一种)内核)等在Python的os模块封装的各个系统的方法调用包括 fork()方法,所以在Python中部分系统也可以通过fork()来创建进程。

from multiprocessing import Pool
import os
print('Process (%s) start...' % os.getpid())
# Only works on Unix/Linux/Mac:
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))
# works on All:
# 子进程要执行的代码
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('Child process will start.')
    #开始进程
    p.start()
    #等待子进程结束后再继续往下运行
    p.join()
    print('Child process end.')

运行结果就不写了,应为我是window系统,第一中通过fork()复制子进程的方法不能运行,fork()与Process()两种方法都可以创建子进程,这样我们就可以通过多个进程来执行多个任务。当然进程模块还有很多方法join()可以实现进程间的同步、还有守护进程等概念。

进程池(Pool)

Python 中提供了进程池来批量创建、管理进程

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

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(4)
    for i in range(5):
        # 创建子进程
        p.apply_async(long_time_task, args=(i,))
    print('Waiting for all subprocesses done...')
    #关闭进程池,关闭后不能添加新的子进程
    p.close()
    #进程间同步,子进程运行完成后,代码继续执行
    p.join()
    print('All subprocesses done.')

输出结果:

Parent process 10624.
Waiting for all subprocesses done...
Run task 0 (6784)...
Run task 1 (11812)...
Run task 2 (740)...
Run task 3 (11048)...
Task 2 runs 0.07 seconds.
Run task 4 (740)...
Task 3 runs 0.30 seconds.
Task 1 runs 0.87 seconds.
Task 4 runs 0.98 seconds.
Task 0 runs 2.50 seconds.
All subprocesses done.

这就是进程池的使用通过apply_async添加子线程,还提供控制线程池的各种方法。

子进程

上面我们介绍了父进程可以fork()出多个子进程,multiprocessing模块中通过 Process()
生成子进程,还有Poolapply_async()批量创建子进程,这几种模式都是子进程对自身的操作,但是很多时候子进程需要执行其他程序或命令,还需要控制子进程的输入输出。这样的子进程我们可以通过subprocess来创建并进程输入输出操作。

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import subprocess
import os
print('Run current process (%s)...' % ( os.getpid()))
print('nslookup www.python.ory')
# 转 utf-8 编码
os.system('chcp 65001')
r = subprocess.call(['nslookup','www.python.org'])
print('Exit code',r)
print('----------------------------------------------')
print('$ nslookup')
print('Run current process (%s)...' % ( os.getpid()))
p = subprocess.Popen(['nslookup'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, err = p.communicate(b'set q=mx\npython.org\nexit\n')
print(output.decode('utf-8'))
print('subprocess.Popen is',p.pid)
print('Exit code:', p.returncode)

输出结果:

Run current process (8172)...
nslookup www.python.ory
Active code page: 65001
Non-authoritative answer:
Server:  public1.114dns.com
Address:  114.114.114.114

Name:    python.map.fastly.net
Addresses:  2a04:4e42:36::223
      151.101.228.223
Aliases:  www.python.org

Exit code 0
----------------------------------------------
$ nslookup
Run current process (8172)...
Default Server:  public1.114dns.com
Address:  114.114.114.114

> > Server:  public1.114dns.com
Address:  114.114.114.114

python.org  MX preference = 50, mail exchanger = mail.python.org

python.org  nameserver = ns3.p11.dynect.net
python.org  nameserver = ns2.p11.dynect.net
python.org  nameserver = ns4.p11.dynect.net
python.org  nameserver = ns1.p11.dynect.net
mail.python.org internet address = 188.166.95.178
mail.python.org AAAA IPv6 address = 2a03:b0c0:2:d0::71:1
ns1.p11.dynect.net  internet address = 208.78.70.11
ns2.p11.dynect.net  internet address = 204.13.250.11
ns3.p11.dynect.net  internet address = 208.78.71.11
ns4.p11.dynect.net  internet address = 204.13.251.11
> 
subprocess.Popen is 15896
Exit code: 0

进程间通信

Python的multiprocessing模块包装了底层的机制,提供了Queue、Pipes等多种方式来交换数据。

做一个Queue通信的示例:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from multiprocessing import Process, Queue
import os, time, random

# 写数据进程执行的代码:
def write(q):
    print('Process to write: %s' % os.getpid())
    for value in ['A', 'B', 'C']:
        print('Put %s to queue...' % value)
        q.put(value)
        time.sleep(random.random())

# 读数据进程执行的代码:
def read(q):
    print('Process to read: %s' % os.getpid())
    while True:
        value = q.get(True)
        print('Get %s from queue.' % value)

if __name__=='__main__':
    # 父进程创建Queue,并传给各个子进程:
    q = Queue()
    pw = Process(target=write, args=(q,))
    pr = Process(target=read, args=(q,))
    # 启动子进程pw,写入:
    pw.start()
    # 启动子进程pr,读取:
    pr.start()
    # 等待pw结束:
    pw.join()
    # pr进程里是死循环,无法等待其结束,只能强行终止:
    pr.terminate()

输出结果:

Process to read: 13760
Process to write: 9296
Put A to queue...
Get A from queue.
Put B to queue...
Get B from queue.
Put C to queue...
Get C from queue.

上面完成了一个读/写 操作,将数据存储在父进程创建的Queue队列中,两个子进程进行写入/读取操作。
进程间的通信方法很多,这这里不深入学习。

多线程

线程是操作系统直接支持的执行单元,因此,高级语言通常都内置多线程的支持,Python也不例外,并且,Python的线程是真正的Posix Thread,而不是模拟出来的线程。Python的标准库提供了两个模块:_thread和threading,_thread是低级模块,threading是高级模块,对_thread进行了封装。绝大多数情况下,我们只需要使用threading这个高级模块。

#!/usr/bin/env python3
# -*- coding: 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().name)

print('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.Thread()就可以创建一个新的线程,执行对应的方法。

Lock

在多进程中,同一个变量,各自有一份拷贝存在于每个进程中,互不影响。而在多线程中,所有变量都由所有线程共享,所以,任何一个变量都可以被任何一个线程修改,这就可能导致执行的结果与预期不符,所以在处理多线程的问题中,出现了一个线程锁。

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import time, threading

# 假定这是你的银行存款:
balance = 0

def change_it(n):
    # 先存后取,结果应该为0:
    global balance
    balance = balance + n
    balance = balance - n

def run_thread(n):
    for i in range(100000):
        change_it(n)

t1 = threading.Thread(target=run_thread, args=(5,))
t2 = threading.Thread(target=run_thread, args=(8,))
t1.start()
t2.start()
t1.join()
t2.join()
print(balance)

以上是两个线程,同时操作同一个函数,逻辑上输出的结果应该是0,但是多次运行会有不同的结果。因为高级语言的一条语句在CPU执行时是若干条语句,所以多个线程同时使用某个变量时,会发生错位的现象。Python中通过threading.Lock()来实现。

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import time, threading

# 假定这是你的银行存款:
balance = 0
lock = threading.Lock()

def change_it(n):
    # 先存后取,结果应该为0:
    global balance
    balance = balance + n
    balance = balance - n

def run_thread(n):
    for i in range(100000):
        # 先要获取锁:
        lock.acquire()
        try:
            # 放心地改吧:
            change_it(n)
        finally:
            # 改完了一定要释放锁:
            lock.release()

t1 = threading.Thread(target=run_thread, args=(5,))
t2 = threading.Thread(target=run_thread, args=(8,))
t1.start()
t2.start()
t1.join()
t2.join()
print(balance)

输出结果:

0

这次无论执行多少次,结果都是0。多个线程同时执行lock.acquire()时,只有一个线程能成功地获取锁,然后继续执行代码,其他线程就继续等待直到获得锁为止。在执行完成一次后一定要释放锁lock.release(),我们用```try...finally...`确保锁被释放,不然会造成死锁

多核CPU

Python解释器由于设计时有GIL全局锁,导致了多线程无法利用多核。Python的线程虽然是真正的线程,但解释器执行代码时,有一个GIL锁:Global Interpreter Lock,任何Python线程执行前,必须先获得GIL锁,然后,每执行100条字节码,解释器就自动释放GIL锁,让别的线程有机会执行。这个GIL全局锁实际上把所有线程的执行代码都给上了锁,所以,多线程在Python中只能交替执行,即使100个线程跑在100核CPU上,也只能用到1个核。Python虽然不能利用多线程实现多核任务,但可以通过多进程实现多核任务。多个Python进程有各自独立的GIL锁,互不影响。

ThreadLocal

ThreadLoacal 可以是一个全局变量,但是每个线程都只能读写自己线程的独立副本,ThreadLocal解决了参数在一个线程中各个函数之间互相传递的问题,而不用考虑管理锁的问题。

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import threading

# 创建全局ThreadLocal对象:
local_school = threading.local()

def process_student():
    # 获取当前线程关联的student:
    std = local_school.student
    print('Hello, %s (in %s)' % (std, threading.current_thread().name))

def process_thread(name):
    # 绑定ThreadLocal的student:
    local_school.student = name
    process_student()

t1 = threading.Thread(target= process_thread, args=('张三',), name='Thread-1')
t2 = threading.Thread(target= process_thread, args=('李四',), name='Thread-2')
t1.start()
t2.start()
t1.join()
t2.join()

输出结果:

Hello, 张三 (in Thread-1)
Hello, 李四 (in Thread-2)

上面实现了student变成一个 local_school对象的属性,每个Thread都可以读取student属性,每个线程读取的都是该线程的局部变量,不会造成错乱,也无需管理锁的问题。ThreadLocal最常用的地方就是为每个线程绑定一个数据库连接,HTTP请求,用户身份信息等配置信息,这样一个线程的所有调用到的处理函数都可以非常方便地访问这些资源。

进程 VS 线程

首先要实现多任务的执行,应该采用Master-Worker模式,Master负责分配任务,Worker负责执行任务:

所以为了缓解这个问题,IIS和Apache现在又有多进程+多线程的混合模式,但这种模式的复杂度更大。

进程/线程切换

无论是多进程还是多线程,只要数量一多,效率肯定上不去 ,因为在进程/线程切换过程中,要进行保护现场、准备新的环境会耗费很多资源、时间。在任务达到一定的限度,就会消耗掉系统所有的资源,结果效率急剧下降,所有任务都做不好。

计算密集型 vs IO密集型

在考虑多任务时,要考虑任务的类型:

异步IO

现代操作系统支持异步IO,单进程单线程模型来执行多任务,这种模型称为事件驱动模型。Nginx就是支持异步IO的Web服务器。对于Python语言,单线程的异步编程模型称为协程,有了协程的支持,就可以基于事件驱动编写高效的多任务程序。

分布式进程

分布式进程只做了解,因为进程是支持分布到多台机器上,而线程是不能的。在Python的multiprocessing模块不但支持多进程,其中managers子模块还支持把多进程分布到多台机器上。一个服务进程可以作为调度者,将任务分布到其他多个进程中,依靠网络通信。Python的分布式进程接口简单,封装良好,适合需要把繁重任务分布到多台机器的环境下。

参考

https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/0014319272686365ec7ceaeca33428c914edf8f70cca383000
http://www.cnblogs.com/Security-Darren/p/4733368.html

上一篇下一篇

猜你喜欢

热点阅读