一、协程
1、又称微线程,纤程。英文名Coroutine.一句话说明什么是协程:协程是一种用户态的轻量级线程(相当于操作系统不知道它的存在,是用户控制的)。
2、协程拥有自己的寄存器上下文和栈(代码的必要的代码段和)。协程调度切换时,将寄存器上下文和栈保存到其他地方,在切回来的时候,恢复先前保存的寄存器上下文和栈,因此:协程能保留上一次调用时的状态(即所有局部状态的一个特定组合),每次过程重入时,就相当于进入上一次调用的状态,换种说法:进入上一次离开时所处逻辑流的位置。
3、协程一定是在单线程中运行的。
二、协程的优点与缺点
优点:
1、无需线程上下文切换的开销。
2、无需原子操作(最小级别的操作)锁定及同步的开销。
3、方便切换控制流,简化编程模型。
4、高并发+高扩展性+低成本:一个CPU支持上万的协程都不是问题,所以很适合用于高并发处理。
缺点:
1、无法利用多核资源:协程的本质是个单线程,它不能同时将单个CPU的多个核用上,协程需要和进程配合才能运行在多CPU上,当然我们日常所编写的绝大部分应用都没有这个必要,除非是cpu密集型应用。
2、进行阻塞(Blocking)操作(如IO时)会阻塞掉整个程序。
三、使用yield实现协程操作例子
1、使用yield实现的一个最简单的协程的效果
#!/usr/bin/python
# -*- coding : utf-8 -*-
# 作者: Presley
# 时间: 2018-12-4
# 邮箱:1209989516@qq.com
# 这是我用来练习python 协程的测试脚本 import time
import queue def consumer(name):
print("starting eating baozi...")
while True:
new_baozi = yield
print("[%s] is eating baozi %s" %(name,new_baozi)) def producer():
r = con.__next__()
r = con2.__next__()
n = 0
while n < 5:
n += 1
con.send(n)
con2.send(n)
print("\033[32;1m[producer]\033[0m is making") if __name__ == "__main__":
con = consumer("c1")
con2 = consumer("c2") p = producer()
执行结果:
C:\Users\wohaoshuai\AppData\Local\Programs\Python\Python36\python.exe E:/PythonLearn/day16/pro_consume.py
starting eating baozi...
starting eating baozi...
[c1] is eating baozi 1
[c2] is eating baozi 1
[producer] is making
[c1] is eating baozi 2
[c2] is eating baozi 2
[producer] is making
[c1] is eating baozi 3
[c2] is eating baozi 3
[producer] is making
[c1] is eating baozi 4
[c2] is eating baozi 4
[producer] is making
[c1] is eating baozi 5
[c2] is eating baozi 5
[producer] is making Process finished with exit code 0
2、greenlet
#!/usr/bin/python
# -*- coding : utf-8 -*-
# 作者: Presley
# 时间: 2018-12-4
# 邮箱:1209989516@qq.com
# 这是我用来练习python 协程的测试脚本 from greenlet import greenlet def test1():
print(12)
gr2.switch()
print(34)
gr2.switch() def test2():
print(56)
gr1.switch()
print(78) gr1 = greenlet(test1)
gr2 = greenlet(test2)
gr1.switch()
执行结果:
C:\Users\wohaoshuai\AppData\Local\Programs\Python\Python36\python.exe E:/PythonLearn/day16/pro_consume.py
12
56
34
78 Process finished with exit code 0
3、gevent
a、gevent是一个第三方库,可以轻松通过gevent实现并发同步或异步编程,在gevent中用到的主要模式是greenlet,它是以C扩展模块形式接入Python的轻量级协程。greenlet全部运行在主程序操作系统进程的内部,但它们被协作式地调度。
#!/usr/bin/python
# -*- coding : utf-8 -*-
# 作者: Presley
# 时间: 2018-12-1
# 邮箱:1209989516@qq.com
# 这是我用来练习python 协程的测试脚本 import gevent def foo():
print("Running in foo")
gevent.sleep(1)
print("Explicit context switch to foo again") def bar():
print("Explicit context to bar")
gevent.sleep(1)
print("Implicit context switch back to bar") def ex():
print("Explicit context to ex")
gevent.sleep(1)
print("Implicit context switch back to ex") gevent.joinall([
gevent.spawn(foo), #类似产生一个协程的foo
gevent.spawn(bar), #产生一个协程的bar
gevent.spawn(ex)
]) #代码的效果为:第一个协程切换到第二个,第二个切换到第三个,然后又遇到sleep(模拟io)又切换到下一个,然后实现并发的协程的效果
执行结果
C:\Users\wohaoshuai\AppData\Local\Programs\Python\Python36\python.exe E:/PythonLearn/day16/pro_consume.py
Running in foo
Explicit context to bar
Explicit context to ex
Explicit context switch to foo again
Implicit context switch back to bar
Implicit context switch back to ex Process finished with exit code 0
b、通过协程爬取网页实例
#!/usr/bin/python
# -*- coding : utf-8 -*-
# 作者: Presley
# 时间: 2018-12-5
# 邮箱:1209989516@qq.com
# 这是我用来练习python 协程的测试脚本 from gevent import monkey;monkey.patch_all()
import gevent from urllib.request import urlopen def f(url):
print("GET: %s" %url)
resp = urlopen(url)
data = resp.read()
print("%d bytes received from %s." %(len(data),url)) gevent.joinall([
gevent.spawn(f,"https://www.python.org/"),
gevent.spawn(f,"https://www.yahoo.com/"),
gevent.spawn(f,"https://github.com"),
])
执行结果:
C:\Users\wohaoshuai\AppData\Local\Programs\Python\Python36\python.exe E:/PythonLearn/day16/pro_consume.py
GET: https://www.python.org/
GET: https://www.yahoo.com/
GET: https://github.com
80704 bytes received from https://github.com.
50008 bytes received from https://www.python.org/.
528149 bytes received from https://www.yahoo.com/. Process finished with exit code 0
c、通过gevent实现单线程下的多socket并发
server端
#!/usr/bin/python
# -*- coding : utf-8 -*-
# 作者: Presley
# 时间: 2018-12-5
# 邮箱:1209989516@qq.com
# 这是我用来练习python 协程的测试脚本 import gevent
from gevent import socket,monkey
monkey.patch_all() #python中的一种黑魔法,只要写入一句话就自动的把python中的许多标准库变为非阻塞的模式 def server(port):
s = socket.socket()
s.bind(("0.0.0.0",port))
s.listen(5000)
while True:
cli,addr = s.accept()
gevent.spawn(handle_request,cli) #执行handle_request函数,参数是cli,即客户端实例
def handle_request(s):
try:
while True:
data = s.recv(1024) #接收数据,这里设置成不阻塞
print("recv:",data)
s.send(data)
if not data:
s.shutdown(socket.SHUT_RD) #如果接收为空值,结束
except Exception as ex:
print(ex)
finally:
s.close() if __name__ == "__main__":
server(8001)
client端
#!/usr/bin/python
# -*- coding : utf-8 -*-
# 作者: Presley
# 时间: 2018-12-5
# 邮箱:1209989516@qq.com
# 这是我用来练习python 协程的测试脚本 import socket HOST = "localhost"
PORT = 8001
s = socket.socket()
s.connect((HOST,PORT)) while True:
msg = input(">>:")
if not msg:continue
msg = msg.encode("utf-8")
s.sendall(msg)
data = s.recv(1024)
print("Received",data.decode("utf-8"))
s.close()