Python 协程(gevent)

时间:2022-11-09 23:04:42

协程,又叫微线程,协程是一种用户态的轻量级线程

协程拥有自己的寄存器上下文和栈。协程调度切换时,将寄存器上下文和栈保存到其他地方,在切回来的时候,恢复先前保存的寄存器上下文和栈。因此:

协程能保留上一次调用时的状态(即所有局部状态的一个特定组合),每次过程重入时,就相当于进入上一次调用的状态,换种说法:进入上一次离开时所处逻辑流的位置。

协程的好处:

  无需线程上下文切换的开销
  无需原子操作锁定及同步的开销
  方便切换控制流,简化编程模型
  高并发+高扩展性+低成本:一个CPU支持上万的协程都不是问题。所以很适合用于高并发处理。
缺点:

  无法利用多核资源:协程的本质是个单线程,它不能同时将 单个CPU 的多个核用上,协程需要和进程配合才能运行在多CPU上.当然我们日常所编写的绝大部分应用都没有这个必要,除非是cpu密集型应用。
  进行阻塞(Blocking)操作(如IO时)会阻塞掉整个程序

用yield实现协程

 import time
import queue def consumer(name):
print("--->starting ...")
while True:
new_baozi = yield
print("[%s] is eating baozi %s" % (name, new_baozi))
# time.sleep(1) def producer():
next(con)
next(con2)
n = 0
while n < 5:
n += 1
print("\033[32;1m[producer]\033[0m is making baozi %s" % n) con.send(n)
con2.send(n) if __name__ == '__main__':
con = consumer("c1") # 创建一个生成器对象con
con2 = consumer("c2") # 创建一个生成器对象con2
p = producer() # 执行producer函数,p就是函数返回值

Gevent

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

需要先安装Gevent库

简单测试

 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() ########## 输出 ##########
12
56
34
78

另一个例子

gevent.sleep() 模拟IO阻塞

import gevent
import time def foo():
print('Running in foo', time.ctime())
gevent.sleep(1)
print('Explicit context switch to foo again', time.ctime()) def bar():
print('Explicit context to bar', time.ctime())
gevent.sleep(2)
print('Implicit context switch back to bar', time.ctime()) gevent.joinall([
gevent.spawn(foo),
gevent.spawn(bar),
])

输出

Running in foo Thu Oct 20 10:52:58 2016
Explicit context to bar Thu Oct 20 10:52:58 2016
Explicit context switch to foo again Thu Oct 20 10:52:59 2016
Implicit context switch back to bar Thu Oct 20 10:53:00 2016

爬网页

 import gevent
import time
from gevent import monkey
monkey.patch_all()
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)) l = ['https://www.python.org/', 'https://www.yahoo.com/', 'https://github.com/']
start = time.time()
# for url in l:
# f(url) # gevent.joinall([
# gevent.spawn(f, 'https://www.pthton.org/'),
# gevent.spawn(f, 'https://www.yahoo.com/'),
# gevent.spawn(f, 'https://github.com/'),
# ]) gevent.joinall([
gevent.spawn(f, 'https://www.bilibili.com/'),
gevent.spawn(f, 'http://weibo.com/'),
gevent.spawn(f, 'http://www.qq.com/'),
]) print(time.time() - start)

socket下的gevent

server

 import sys
import socket
import time
import gevent from gevent import socket, monkey monkey.patch_all() def server(port):
s = socket.socket()
s.bind(('0.0.0.0', port))
s.listen(500) while True:
conn, addr = s.accept()
gevent.spawn(handle_request, conn) def handle_request(conn):
try:
while True:
data = conn.recv(1024)
print("recv:", data)
conn.send(data)
if not data:
conn.shutdown(socket.SHUT_WR)
#break except Exception as ex:
print(ex)
finally:
conn.close() if __name__ == '__main__':
server(8001)

clinent

 import socket

 HOST = 'localhost'  # The remote host
PORT = 8001 # The same port as used by the server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT)) while True:
msg = bytes(input(">>:"), encoding="utf8")
s.sendall(msg)
data = s.recv(1024)
# print(data)
print('Received', str(data,'utf8')) s.close()