专题13:一文让你彻底明白python中的进程、线程、协程、GIL

文 章 内 容 可 能 会 比 较 长 , 针 对 每 一 部 分 的 内 容 , 建 议 深 入 了 解 , 我 之 所 以 把 这 些 知 识 点 放 在 一 起 解 析 , \color{red}{文章内容可能会比较长,针对每一部分的内容,建议深入了解,我之所以把这些知识点放在一起解析,}
一 是 想 比 较 清 晰 的 整 理 出 来 , 遇 到 问 题 不 用 到 处 找 , 二 是 坚 持 学 完 p y t h o n 中 的 这 一 部 分 知 识 , 不 要 只 是 一 知 半 解 。 \color{purple}{ 一是想比较清晰的整理出来,遇到问题不用到处找,二是坚持学完python中的这一部分知识,不要只是一知半解。} python
如 果 有 任 何 的 建 议 和 意 见 , 或 者 觉 得 那 一 部 分 有 问 题 可 以 及 时 的 在 评 论 中 提 出 . \color{#666}{ 如果有任何的建议和意见,或者觉得那一部分有问题可以及时的在评论中提出.} .

进程

进程介绍

进程就是一个程序在一个数据集上的一次动态执行过程。 进程一般由程序、数据集、进程控制块三部分组成。我们编写的程序用来描述进程要完成哪些功能以及如何完成;数据集则是程序在执行过程中所需要使用的资源;进程控制块用来记录进程的外部特征,描述进程的执行变化过程,系统可以利用它来控制和管理进程,它是系统感知进程存在的唯一标志。

multiprocessing

multiprocessing是python的多进程管理包, 接下来我们分析一下这个包的功能。
multiprocessing并非是python的一个模块,而是python中多进程管理的一个包。
multiprocessing模块用来开启子进程,并在子进程中执行我们定制的任务(比如函数),该模块与多线程模块threading的编程接口类似。

Process 创建进程

创建方式:

  • 实例化一个multiprocessing.Process的对象,并传入一个初始化函数对象(initial function )作为新建进程执行入口;
  • 继承multiprocessing.Process,并重写run函数;

roup参数未使用,值始终为None
target表示调用对象,即子进程要执行的任务
args表示调用对象的位置参数元组,args=(1,2,‘egon’,)
kwargs表示调用对象的字典,kwargs={‘name’:‘egon’,‘age’:18}
name为子进程的名称

# 方式一
from multiprocessing import Process  
import os, time

def pstart(name):
    # time.sleep(0.1)
    print("Process name: %s, pid: %s "%(name, os.getpid()))

if __name__ == "__main__": 
    subproc = Process(target=pstart, args=('subprocess',))  
    subproc.start()  
    subproc.join()
    print("subprocess pid: %s"%subproc.pid)
    print("current process pid: %s" % os.getpid())
# 方式二
from multiprocessing import Process  
import os, time

class CustomProcess(Process):
    def __init__(self, p_name, target=None):
        # step 1: call base __init__ function()
        super(CustomProcess, self).__init__(name=p_name, target=target, args=(p_name,))

    def run(self):
        # step 2:
        # time.sleep(0.1)
        print("Custom Process name: %s, pid: %s "%(self.name, os.getpid()))

if __name__ == '__main__':
    p1 = CustomProcess("process_1")
    p1.start()
    p1.join()
    print("subprocess pid: %s"%p1.pid)
    print("current process pid: %s" % os.getpid())

Queue 进程通信

进程间通信
线程间共享内存空间,进程间只能通过其他方法进行通信
注意这个 Queue 不同于 queue.Queue
Queue type using a pipe, buffer and thread
两个进程的 Queue 并不是同一个,而是将数据 pickle 后传给另一个进程的 Queue
用于父进程与子进程之间的通信或同一父进程的子进程之间通信

from multiprocessing import Process, Queue

def p_put(*args):
    q.put(args)
    print('Has put %s' % args)

def p_get(*args):
    print('%s wait to get...' % args)
    print(q.get())
    print('%s got it' % args)

q = Queue()
p1 = Process(target=p_put, args=('p1', ))
p2 = Process(target=p_get, args=('p2', ))
p1.start()
p2.start()

#输出结果:
#Has put p1
#p2 wait to get...
#('p1',)
#p2 got it
from multiprocessing import Process
import queue

def p_put(*args):
    q.put(args)
    print('Has put %s' % args)

def p_get(*args):
    print('%s wait to get...' % args)
    print(q.get())
    print('%s got it' % args)

q = queue.Queue()
p1 = Process(target=p_put, args=('p1', ))
p2 = Process(target=p_get, args=('p2', ))
p1.start()
p2.start()

#输出结果:
#Has put p1
#p2 wait to get...
#由于父进程启动子进程时是复制一份,所以每个子进程里也有一个空的队列,但是这些队列数据独立,所以 get 时会阻塞

Pipe 进程间通信

Pipe(管道) 是通过 socket 进行进程间通信的
所以步骤与建立 socket 连接相似:
建立连接、发送/接收数据(一端发送另一端不接受就会阻塞)、关闭连接

from multiprocessing import Pipe, Process

def f(conn):
    conn.send('send by child')
    print('child recv:', conn.recv())
    conn.close()

parent_conn, child_conn = Pipe()	# 获得 Pipe 连接的两端
p = Process(target=f, args=(child_conn, ))
p.start()
print('parent recv:', parent_conn.recv())
parent_conn.send('send by parent')
p.join()

Manger 数据共享

进程间数据共享
Manager 实现的是进程间共享数据

from multiprocessing import Manager, Process
import os

def func():
    m_dict['key'] = 'value'
    m_list.append(os.getpid())

manager = Manager()
m_dict = manager.dict()
m_list = manager.list()
p_list = []
for i in range(10):
    p = Process(target=func)
    p.start()
    p_list.append(p)
for p in p_list:
    p.join()
print(m_list)
print(m_dict)

支持的数据类型

list
dict
Value
Array
Namespace
Queue 				queue.Queue
JoinableQueue		queue.Queue
Event 				threading.Event
Lock 				threading.Lock
RLock 				threading.RLock
Semaphore 			threading.Semaphore
BoundedSemaphore 	threading.BoundedSemaphore
Condition 			threading.Condition
Barrier 			threading.Barrier
Pool 				pool.Pool

Lock 进程锁

from multiprocessing import Lock, Process

def foo(n, l):
    l.acquire()
    print('hello world', n)
    l.release()

lock = Lock()
for i in range(100):
    process = Process(target=foo, args=(i, lock))
    process.start()

Pool 进程池

同一时间多个进程在CPU运行

from multiprocessing import Pool
import time
import os


def foo(n):
    time.sleep(1)
    print('In process', n, os.getpid())
    return n


def bar(*args):
    print('>>done: ', args, os.getpid())


if __name__ == '__main__':
    pool = Pool()
    print('主进程: ', os.getpid())
    for i in range(10):
        # pool.apply(func=foo, args=(i, ))
        pool.apply_async(func=foo, args=(i,), callback=bar)
    print('end')
    pool.close()
    pool.join()

从程序运行过程中可以看出:同一时间最多只有3个进程在运行,类似于线程中的信号量
主进程在执行 callback 函数

注意
pool.apply(func=foo, args=(i, )) 是串行执行
pool.apply_async(func=foo, args=(i, ), callback=bar) 是并行执行

callback 函数会以 target 函数返回结果为参数,在 target 函数执行结束之后执行
callback 函数是主进程调用的

如果不执行 join,程序会在主进程执行完成之后直接结束,不会等待子进程执行完成
Pool.join() 必须在 Pool.close() 之后执行,否则会报错:ValueError: Pool is still running

线程

线程介绍

线程也叫轻量级进程,它是一个基本的CPU执行单元,也是程序执行过程中的最小单元,由线程ID、程序计数器、寄存器集合 和堆栈共同组成。线程的引入减小了程序并发执行时的开销,提高了操作系统的并发性能。 线程没有自己的系统资源,只拥有在运行时必不可少的资源。但线程可以与同属与同一进程的其他线程共享进程所拥有的其他资源。

threading

在这里插入图片描述

python中提供两个标准库thread和threading用于对线程的支持,python3中已放弃对前者的支持,后者是一种更高层次封装的线程库,接下来均以后者为例。

创建线程

# 方式一
import threading
import time
def worker(num):
	"""
	thread worker function
	:return:
	"""
	time.sleep(1)
	print("The num is %d" % num)
	return
for i in range(20):
	t = threading.Thread(target=worker,args=(i,),name=“t.%d” % i)
t.start()
# 方式二
import threading
import time

class CustomThread(threading.Thread):
    def __init__(self, thread_name):
        # step 1: call base __init__ function
        super(CustomThread, self).__init__(name=thread_name)
        self._tname = thread_name

    def run(self):
        # step 2: overide run function
        time.sleep(0.5)
        print("This is %s running...." % self._tname)

if __name__ == "__main__":
    t1 = CustomThread("thread 1")
    t2 = CustomThread("thread 2")
    t1.start()
    t2.start()
    print("This is main function")

上面两种方法本质上都是直接或者间接使用threading.Thread类

threading.Thread(group=None, target=None, name=None, args=(), kwargs={})

Thread方法说明
t.start() : 激活线程,创建线程后通过start启动线程,等待CPU调度,为run函数执行做准备;,
t.getName() : 获取线程的名称
t.setName() : 设置线程的名称
t.name : 获取或设置线程的名称
t.is_alive() : 判断线程是否为激活状态
t.isAlive() :判断线程是否为激活状态
t.setDaemon() 设置为后台线程或前台线程(默认:False);通过一个布尔值设置线程是否为守护线程,必须在执行start()方法之后才可以使用。如果是后台线程,主线程执行过程中,后台线程也在进行,主线程执行完毕后,后台线程不论成功与否,均停止;如果是前台线程,主线程执行过程中,前台线程也在进行,主线程执行完毕后,等待前台线程也执行完成后,程序停止
t.isDaemon() : 判断是否为守护线程
t.ident :获取线程的标识符。线程标识符是一个非零整数,只有在调用了start()方法之后该属性才有效,否则它只返回None。
t.join() :逐个执行每个线程,执行完毕后继续往下执行,该方法使得多线程变得无意义 阻塞挂起调用该函数的线程,直到被调用线程执行完成或超时。通常会在主线程中调用该方法,等待其他线程执行完成
t.run() :线程被cpu调度后自动执行线程对象的run方法 线程开始执行的入口函数,函数体中会调用用户编写的target函数,或者执行被重载的run函数;

线程锁threading.RLock和threading.Lock

由于线程之间是进行随机调度,并且每个线程可能只执行n条执行之后,CPU接着执行其他线程。为了保证数据的准确性,引入了锁的概念。所以,可能出现如下问题:

例:假设列表A的所有元素就为0,当一个线程从前向后打印列表的所有元素,另外一个线程则从后向前修改列表的元素为1,那么输出的时候,列表的元素就会一部分为0,一部分为1,这就导致了数据的不一致。锁的出现解决了这个问题。

import threading
import time
globals_num = 0
lock = threading.RLock()
def Func():
	lock.acquire() # 获得锁 
	global globals_num
	globals_num += 1
	time.sleep(1)
	print(globals_num)
	lock.release() # 释放锁 
for i in range(10):
	t = threading.Thread(target=Func)
t.start()

互斥锁有三个常用步骤

  • lock = threading.Lock() # 取得锁
  • lock.acquire() # 上锁
  • lock.release() # 解锁
import threading
from time import *

class MyThread(threading.Thread):
    def __init__(self,counter,name,lock):
        threading.Thread.__init__(self)  
        self.counter = counter
        self.name = name
        self.lock = lock

    def run(self):
        self.lock.acquire()
        self.counter[0] += 1
        sleep(1)
        print self.counter[0]
        self.lock.release()

if __name__ == '__main__':
    counter = [0]
    lock = threading.Lock()
    for i in range(1,100):
        t = MyThread(counter,i,lock)
        t.start()

threading.RLock和threading.Lock 的区别
RLock允许在同一线程中被多次acquire。而Lock却不允许这种情况。 如果使用RLock,那么acquire和release必须成对出现,即调用了n次acquire,必须调用n次的release才能真正释放所占用的琐。

threading.Event

python线程的事件用于主线程控制其他线程的执行,事件主要提供了三个方法 set、wait、clear。
事件处理的机制:全局定义了一个“Flag”,如果“Flag”值为 False,那么当程序执行 event.wait 方法时就会阻塞,如果“Flag”值为True,那么event.wait 方法时便不再阻塞。

  • clear:将“Flag”设置为False
  • set:将“Flag”设置为True
  • Event.isSet() :判断标识位是否为Ture
#利用Event类模拟红绿灯
import threading
import time

event = threading.Event()


def lighter():
    count = 0
    event.set()     #初始值为绿灯
    while True:
        if 5 < count <=10 :
            event.clear()  # 红灯,清除标志位
            print("\33[41;1mred light is on...\033[0m")
        elif count > 10:
            event.set()  # 绿灯,设置标志位
            count = 0
        else:
            print("\33[42;1mgreen light is on...\033[0m")

        time.sleep(1)
        count += 1

def car(name):
    while True:
        if event.is_set():      #判断是否设置了标志位
            print("[%s] running..."%name)
            time.sleep(1)
        else:
            print("[%s] sees red light,waiting..."%name)
            event.wait()
            print("[%s] green light is on,start going..."%name)

light = threading.Thread(target=lighter,)
light.start()

car = threading.Thread(target=car,args=("MINI",))
car.start()

当线程执行的时候,如果flag为False,则线程会阻塞,当flag为True的时候,线程不会阻塞。它提供了本地和远程的并发性。

threading.Condition

一个condition变量总是与某些类型的锁相联系,这个可以使用默认的情况或创建一个,当几个condition变量必须共享和同一个锁的时候,是很有用的。锁是conditon对象的一部分:没有必要分别跟踪。

condition变量服从上下文管理协议:with语句块封闭之前可以获取与锁的联系。 acquire() 和 release() 会调用与锁相关联的相应的方法。

其他和锁关联的方法必须被调用,wait()方法会释放锁,当另外一个线程使用 notify() or notify_all()唤醒它之前会一直阻塞。一旦被唤醒,wait()会重新获得锁并返回

Condition类实现了一个conditon变量。 这个conditiaon变量允许一个或多个线程等待,直到他们被另一个线程通知。 如果lock参数,被给定一个非空的值,,那么他必须是一个lock或者Rlock对象,它用来做底层锁。否则,会创建一个新的Rlock对象,用来做底层锁.

acquire(): 线程锁
release(): 释放锁
wait(timeout): 线程挂起,直到收到一个notify通知或者超时(可选的,浮点数,单位是秒s)才会被唤醒继续运行。wait()必须在已获得Lock前提下才能调用,否则会触发RuntimeError。
notify(n=1): 通知其他线程,那些挂起的线程接到这个通知之后会开始运行,默认是通知一个正等待该condition的线程,最多则唤醒n个等待的线程。notify()必须在已获得Lock前提下才能调用,否则会触发RuntimeError。notify()不会主动释放Lock。
notifyAll(): 如果wait状态线程比较多,notifyAll的作用就是通知所有线程

import threading
from time import *

class MyThread(threading.Thread):
    def __init__(self,counter,name,con):
        threading.Thread.__init__(self)  
        self.counter = counter
        self.name = name
        self.con = con

    def run(self):
        self.con.acquire()
        self.counter[0] += 1
        sleep(1)
        print self.counter[0]
        con.notify()        
        con.wait()
        self.con.release()

if __name__ == '__main__':
    counter = [0]
    con = threading.Condition()
    for i in range(1,100):
        t = MyThread(counter,i,con)
        t.start()

threading.BoundedSemaphore

互斥锁同时只允许一个线程更改数据,而Semaphore信号量是同时允许一定数量的线程更改数据 ,比如厕所有3个坑,那最多只允许3个人上厕所,后面的人只能等里面有人出来了才能再进去。

import threading
from time import *

class MyThread(threading.Thread):
    def __init__(self,counter,name):
        threading.Thread.__init__(self)  
        self.counter = counter
        self.name = name


    def run(self):
        semaphore.acquire()
        self.counter[0] += 1
        sleep(1)
        print self.counter[0]
        semaphore.release()

if __name__ == '__main__':
    counter = [0]
    semaphore = threading.BoundedSemaphore(5) 
    for i in range(1,100):
        t = MyThread(counter,i)
        t.start()

threading.Timer

指定多长时间之后的操作

from threading import Timer
 
 
def hello():
    print("hello, world")
 
t = Timer(1, hello)
t.start()  # after 1 seconds, "hello, world" will be printed

线程池

from concurrent.futures import ProcessPoolExecutor,ThreadPoolExecutor
import time,os,random

def task(x):
    print('%s 接客' %x)
    time.sleep(random.randint(2,5))
    return x**2

if __name__ == '__main__':
    p=ThreadPoolExecutor(4) # 默认开启的线程数是cpu的核数*5
    for i in range(20):
        p.submit(task,i)

多进程和多线程的比较

进程是资源分配的基本单位,线程是CPU执行和调度的基本单位;
通信/同步方式:

  • 进程:
    • 通信方式:管道,FIFO,消息队列,信号,共享内存,socket,stream流;
    • 同步方式:PV信号量,管程
  • 线程:
    • 同步方式:互斥锁,递归锁,条件变量,信号量
    • 通信方式:位于同一进程的线程共享进程资源,因此线程间没有类似于进程间用于数据传递的通信方式,线程间的通信主要是用于线程同步。

CPU上真正执行的是线程,线程比进程轻量,其切换和调度代价比进程要小;

线程间对于共享的进程数据需要考虑线程安全问题,由于进程之间是隔离的,拥有独立的内存空间资源,相对比较安全,只能通过上面列出的IPC(Inter-Process Communication)进行数据传输;

系统有一个个进程组成,每个进程包含代码段、数据段、堆空间和栈空间,以及操作系统共享部分 ,有等待,就绪和运行三种状态;

一个进程可以包含多个线程,线程之间共享进程的资源(文件描述符、全局变量、堆空间等),寄存器变量和栈空间等是线程私有的;

操作系统中一个进程挂掉不会影响其他进程,如果一个进程中的某个线程挂掉而且OS对线程的支持是多对一模型,那么会导致当前进程挂掉;

如果CPU和系统支持多线程与多进程,多个进程并行执行的同时,每个进程中的线程也可以并行执行,这样才能最大限度的榨取硬件的性能;

使用多线程还是多进程?

  • CPU密集型:程序需要占用CPU进行大量的运算和数据处理;
  • I/O密集型:程序中需要频繁的进行I/O操作;例如网络中socket数据传输和读取等;

由于python多线程并不是并行执行,因此较适合与I/O密集型程序,多进程并行执行适用于CPU密集型程序;

GIL

参考文章1
参考文章2

背景

'''
定义:
In CPython, the global interpreter lock, or GIL, is a mutex that prevents multiple 
native threads from executing Python bytecodes at once. This lock is necessary mainly 
because CPython’s memory management is not thread-safe. (However, since the GIL 
exists, other features have grown to depend on the guarantees that it enforces.)
'''
结论:在Cpython解释器中,同一个进程下开启的多线程,同一时刻只能有一个线程执行,无法利用多核优势

首先需要明确的一点是GIL并不是Python的特性,它是在实现Python解析器(CPython)时所引入的一个概念。就好比C++是一套语言(语法)标准,但是可以用不同的编译器来编译成可执行代码。有名的编译器例如GCC,INTEL C++,Visual C++等。Python也一样,同样一段代码可以通过CPython,PyPy,Psyco等不同的Python执行环境来执行。像其中的JPython就没有GIL。然而因为CPython是大部分环境下默认的Python执行环境。所以在很多人的概念里CPython就是Python,也就想当然的把GIL归结为Python语言的缺陷。所以这里要先明确一点:GIL并不是Python的特性,Python完全可以不依赖于GIL.
GIL对python多线程的影响

介绍

GIL本质就是一把互斥锁,既然是互斥锁,所有互斥锁的本质都一样,都是将并发运行变成串行,以此来控制同一时间内共享数据只能被一个任务所修改,进而保证数据安全。

可以肯定的一点是:保护不同的数据的安全,就应该加不同的锁。

要想了解GIL,首先确定一点:每次执行python程序,都会产生一个独立的进程。例如python test.py,python aaa.py,python bbb.py会产生3个不同的python进程

如果多个线程的target=work,那么执行流程是
多个线程先访问到解释器的代码,即拿到执行权限,然后将target的代码交给解释器的代码去执行

解释器的代码是所有线程共享的,所以垃圾回收线程也可能访问到解释器的代码而去执行,这就导致了一个问题:对于同一个数据100,可能线程1执行x=100的同时,而垃圾回收执行的是回收100的操作,解决这种问题没有什么高明的方法,就是加锁处理,如下图的GIL,保证python解释器同一时间只能执行一个任务的代码
在这里插入图片描述

GIL和Lock

在这里插入图片描述

GIL和线程

#分析:
我们有四个任务需要处理,处理方式肯定是要玩出并发的效果,解决方案可以是:
方案一:开启四个进程
方案二:一个进程下,开启四个线程

#单核情况下,分析结果:
  如果四个任务是计算密集型,没有多核来并行计算,方案一徒增了创建进程的开销,方案二胜
  如果四个任务是I/O密集型,方案一创建进程的开销大,且进程的切换速度远不如线程,方案二胜

#多核情况下,分析结果:
  如果四个任务是计算密集型,多核意味着并行计算,在python中一个进程中同一时刻只有一个线程执行用不上多核,方案一胜
  如果四个任务是I/O密集型,再多的核也解决不了I/O问题,方案二胜

#结论:现在的计算机基本上都是多核,python对于计算密集型的任务开多线程的效率并不能带来多大性能上的提升,甚至不如串行(没有大量切换),但是,对于IO密集型的任务效率还是有显著提升的。

协程

线程和进程的操作是由程序触发系统接口,最后的执行者是系统;协程的操作则是程序员。

协程存在的意义:对于多线程应用,CPU通过切片的方式来切换线程间的执行,线程切换时需要耗时(保存状态,下次继续)。协程,则只使用一个线程,在一个线程中规定某个代码块执行顺序。

协程的适用场景:当程序中存在大量不需要CPU的操作时(IO),适用于协程;

实现方式

在Python中有多种方式可以实现协程,例如:

  • greenlet,是一个第三方模块,用于实现协程代码(Gevent协程就是基于greenlet实现)
  • yield,生成器,借助生成器的特点也可以实现协程代码。
  • asyncio,在Python3.4中引入的模块用于编写协程代码。
  • async & awiat,在Python3.5中引入的两个关键字,结合asyncio模块可以更方便的编写协程代码。

greenlet

greentlet是一个第三方模块,需要提前安装 pip3 install greenlet才能使用。

from greenlet import greenlet
def func1():
    print(1)        # 第1步:输出 1
    gr2.switch()    # 第3步:切换到 func2 函数
    print(2)        # 第6步:输出 2
    gr2.switch()    # 第7步:切换到 func2 函数,从上一次执行的位置继续向后执行
def func2():
    print(3)        # 第4步:输出 3
    gr1.switch()    # 第5步:切换到 func1 函数,从上一次执行的位置继续向后执行
    print(4)        # 第8步:输出 4
gr1 = greenlet(func1)
gr2 = greenlet(func2)
gr1.switch() # 第1步:去执行 func1 函数

yield

基于Python的生成器的yield和yield form关键字实现协程代码。

def func1():
    yield 1
    yield from func2()
    yield 2
def func2():
    yield 3
    yield 4
f1 = func1()
for item in f1:
    print(item)

asyncio

import asyncio
@asyncio.coroutine
def func1():
    print(1)
    yield from asyncio.sleep(2)  # 遇到IO耗时操作,自动化切换到tasks中的其他任务
    print(2)
@asyncio.coroutine
def func2():
    print(3)
    yield from asyncio.sleep(2) # 遇到IO耗时操作,自动化切换到tasks中的其他任务
    print(4)
tasks = [
    asyncio.ensure_future( func1() ),
    asyncio.ensure_future( func2() )
]
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait(tasks))

注意:基于asyncio模块实现的协程比之前的要更厉害,因为他的内部还集成了遇到IO耗时操作自动切花的功能。

async & wait

async & awit 关键字在Python3.5版本中正式引入,基于他编写的协程代码其实就是 上一示例 的加强版,让代码可以更加简便。
Python3.8之后 @asyncio.coroutine 装饰器就会被移除,推荐使用async & awit 关键字实现协程代码。

import asyncio
async def func1():
    print(1)
    await asyncio.sleep(2)
    print(2)
async def func2():
    print(3)
    await asyncio.sleep(2)
    print(4)
tasks = [
    asyncio.ensure_future(func1()),
    asyncio.ensure_future(func2())
]
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait(tasks))

Gevent

Gevent 是一个第三方库,可以轻松通过gevent实现并发同步或异步编程,在gevent中用到的主要模式是Greenlet, 它是以C扩展模块形式接入Python的轻量级协程。 Greenlet全部运行在主程序操作系统进程的内部,但它们被协作式地调度。

# -*- coding:utf-8 -*-
import gevent
 
def func1():
    print('\033[31;1m李闯在跟海涛搞...\033[0m')
    gevent.sleep(2)
    print('\033[31;1m李闯又回去跟继续跟海涛搞...\033[0m')
 
def func2():
    print('\033[32;1m李闯切换到了跟海龙搞...\033[0m')
    gevent.sleep(1)
    print('\033[32;1m李闯搞完了海涛,回来继续跟海龙搞...\033[0m')
 
 
gevent.joinall([
    gevent.spawn(func1),
    gevent.spawn(func2),
    #gevent.spawn(func3),
])

协程的好处

  • 无需线程上下文切换的开销
  • 无需原子操作锁定及同步的开销
      “原子操作(atomic operation)是不需要synchronized”,所谓原子操作是指不会被线程调度机制打断的操作;这种操作一旦开始,就一直运行到结束,中间不会有任何 context switch (切换到另一个线程)。原子操作可以是一个步骤,也可以是多个操作步骤,但是其顺序是不可以被打乱,或者切割掉只执行部分。视作整体是原子性的核心。
  • 方便切换控制流,简化编程模型
  • 高并发+高扩展性+低成本:一个CPU支持上万的协程都不是问题。所以很适合用于高并发处理。

进程、线程、协程

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值