Gevent的介绍
greenlet已经实现了协程,但是这个还要人工切换,这里介绍一个比greenlet更强大而且能够自动切换任务的第三方库,那就是gevent。
gevent内部封装的greenlet,其原理是当一个greenlet遇到IO操作时,,比如访问网络,就自动切换到其他的greenlet,等到IO操作完成,再在适当的时候切换回来继续执行。
由于IO操作非常耗时,经常是程序处于等待状态,有了gevent为我们自动切换协程,就保证总有greenlet在运行,而不是等待IO。
安装
pip install gevent
Gevent的使用
代码示例:
import gevent
def work(n):
for i in range(n):
# 获取当前协程
print(gevent.getcurrent(), i)
return 'ok'
g1 = gevent.spawn(work, 5)
g2 = gevent.spawn(work, 5)
g3 = gevent.spawn(work, 5)
g1.join()
g2.join()
g3.join()
# 或者上述三步合作一步:gevent.joinall([g1,g2,g3])
# 拿到work的返回值
result = g1.value
print(result)
# ok
结果:
<Greenlet at 0x16be40925c0: work(5)> 0
<Greenlet at 0x16be40925c0: work(5)> 1
<Greenlet at 0x16be40925c0: work(5)> 2
<Greenlet at 0x16be40925c0: work(5)> 3
<Greenlet at 0x16be40925c0: work(5)> 4
<Greenlet at 0x16be44609a0: work(5)> 0
<Greenlet at 0x16be44609a0: work(5)> 1
<Greenlet at 0x16be44609a0: work(5)> 2
<Greenlet at 0x16be44609a0: work(5)> 3
<Greenlet at 0x16be44609a0: work(5)> 4
<Greenlet at 0x16be4460860: work(5)> 0
<Greenlet at 0x16be4460860: work(5)> 1
<Greenlet at 0x16be4460860: work(5)> 2
<Greenlet at 0x16be4460860: work(5)> 3
<Greenlet at 0x16be4460860: work(5)> 4
可以看到,3个greenlet是依次运行而不是交替运行
Gevent切换执行
遇到IO阻塞时会自动切换任务
import gevent
def work(n):
for i in range(n):
# 获取当前协程
print(gevent.getcurrent(), i)
#用来模拟一个耗时操作,注意不是time模块中的sleep
gevent.sleep(1)
g1 = gevent.spawn(work, 5)
g2 = gevent.spawn(work, 5)
g3 = gevent.spawn(work, 5)
g1.join()
g2.join()
g3.join()
结果:
<Greenlet at 0x7fa70ffa1c30: f(5)> 0
<Greenlet at 0x7fa70ffa1870: f(5)> 0
<Greenlet at 0x7fa70ffa1eb0: f(5)> 0
<Greenlet at 0x7fa70ffa1c30: f(5)> 1
<Greenlet at 0x7fa70ffa1870: f(5)> 1
<Greenlet at 0x7fa70ffa1eb0: f(5)> 1
<Greenlet at 0x7fa70ffa1c30: f(5)> 2
<Greenlet at 0x7fa70ffa1870: f(5)> 2
<Greenlet at 0x7fa70ffa1eb0: f(5)> 2
<Greenlet at 0x7fa70ffa1c30: f(5)> 3
<Greenlet at 0x7fa70ffa1870: f(5)> 3
<Greenlet at 0x7fa70ffa1eb0: f(5)> 3
<Greenlet at 0x7fa70ffa1c30: f(5)> 4
<Greenlet at 0x7fa70ffa1870: f(5)> 4
<Greenlet at 0x7fa70ffa1eb0: f(5)> 4
猴子补丁(兼容其他IO)
而time.sleep(2)或其他的阻塞,gevent是不能直接识别的需要用下面一行代码,打补丁,就可以识别了。
from gevent import monkey;monkey.patch_all()必须放到被打补丁者的前面,如time,socket模块之前,或者我们干脆记忆成:要用gevent,需要将from gevent import monkey; monkey.patch_all()放到文件的开头。
from gevent import monkey
monkey.patch_all() # # 打补丁,让gevent框架识别耗时操作,比如:time.sleep,网络请求延时
import threading
import gevent
import time
def work(n):
for i in range(n):
# 获取当前协程
print(gevent.getcurrent(), i)
print(threading.current_thread())
# 用来模拟一个耗时操作,注意不是time模块中的sleep
time.sleep(1)
if __name__ == '__main__':
g1 = gevent.spawn(work, 3)
g2 = gevent.spawn(work, 3)
g3 = gevent.spawn(work, 3)
g1.join()
g2.join()
g3.join()
print(threading.current_thread())
结果:
<Greenlet at 0x1fa4a1a3e20: work(3)> 0
<_DummyThread(Dummy-1, started daemon 2174496685600)>
<Greenlet at 0x1fa4a2b1120: work(3)> 0
<_DummyThread(Dummy-2, started daemon 2174497788192)>
<Greenlet at 0x1fa4a2c13a0: work(3)> 0
<_DummyThread(Dummy-3, started daemon 2174497854368)>
<Greenlet at 0x1fa4a1a3e20: work(3)> 1
<_DummyThread(Dummy-1, started daemon 2174496685600)>
<Greenlet at 0x1fa4a2b1120: work(3)> 1
<_DummyThread(Dummy-2, started daemon 2174497788192)>
<Greenlet at 0x1fa4a2c13a0: work(3)> 1
<_DummyThread(Dummy-3, started daemon 2174497854368)>
<Greenlet at 0x1fa4a1a3e20: work(3)> 2
<_DummyThread(Dummy-1, started daemon 2174496685600)>
<Greenlet at 0x1fa4a2b1120: work(3)> 2
<_DummyThread(Dummy-2, started daemon 2174497788192)>
<Greenlet at 0x1fa4a2c13a0: work(3)> 2
<_DummyThread(Dummy-3, started daemon 2174497854368)>
<_MainThread(MainThread, started 2174463857152)>
我们可以用threading.current_thread()来查看每个g1、g2和g3,查看的结果为DummyThread-n,即假线程
注意
当前程序是一个死循环并且还能有耗时操作,就不需要加上join方法了,因为程序需要一直运行不会退出。
代码示例:
import gevent
import time
from gevent import monkey
# 打补丁,让gevent框架识别耗时操作,比如:time.sleep,网络请求延时
monkey.patch_all()
# 任务1
def work1(num):
for i in range(num):
print("work1....")
time.sleep(0.2)
# gevent.sleep(0.2)
# 任务1
def work2(num):
for i in range(num):
print("work2....")
time.sleep(0.2)
# gevent.sleep(0.2)
if __name__ == '__main__':
# 创建协程指定对应的任务
g1 = gevent.spawn(work1, 3)
g2 = gevent.spawn(work2, 3)
while True:
print("主线程中执行")
time.sleep(0.5)
结果:
主线程中执行
work1....
work2....
work1....
work2....
work1....
work2....
主线程中执行
主线程中执行
主线程中执行
..省略..
Gevent之同步与异步
from gevent import spawn,joinall,monkey;monkey.patch_all()
import time
def task(pid):
"""
Some non-deterministic task
"""
time.sleep(0.5)
print('Task %s done' % pid)
def synchronous():
for i in range(10):
task(i)
def asynchronous():
g_l=[spawn(task,i) for i in range(10)]
joinall(g_l)
if __name__ == '__main__':
print('Synchronous:')
synchronous()
print('Asynchronous:')
asynchronous()
#上面程序的重要部分是将task函数封装到Greenlet内部线程的gevent.spawn。 初始化的greenlet列表存放在数组threads中,此数组被传给gevent.joinall 函数,后者阻塞当前流程,并执行所有给定的greenlet。执行流程只会在 所有greenlet执行完后才会继续向下走。
Gevent之应用举例一
协程应用:爬虫
from gevent import monkey;monkey.patch_all()
import gevent
import requests
import time
def get_page(url):
print('GET: %s' %url)
response=requests.get(url)
if response.status_code == 200:
print('%d bytes received from %s' %(len(response.text),url))
start_time=time.time()
gevent.joinall([
gevent.spawn(get_page,'https://www.python.org/'),
gevent.spawn(get_page,'https://www.yahoo.com/'),
gevent.spawn(get_page,'https://github.com/'),
])
stop_time=time.time()
print('run time is %s' %(stop_time-start_time))
Gevent之应用举例二
通过gevent实现单线程下的socket并发(from gevent import monkey;monkey.patch_all()一定要放到导入socket模块之前,否则gevent无法识别socket的阻塞。
服务端:
from gevent import monkey;monkey.patch_all()
from socket import *
import gevent
#如果不想用money.patch_all()打补丁,可以用gevent自带的socket
# from gevent import socket
# s=socket.socket()
def server(server_ip,port):
s=socket(AF_INET,SOCK_STREAM)
s.setsockopt(SOL_SOCKET,SO_REUSEADDR,1)
s.bind((server_ip,port))
s.listen(5)
while True:
conn,addr=s.accept()
gevent.spawn(talk,conn,addr)
def talk(conn,addr):
try:
while True:
res=conn.recv(1024)
print('client %s:%s msg: %s' %(addr[0],addr[1],res))
conn.send(res.upper())
except Exception as e:
print(e)
finally:
conn.close()
if __name__ == '__main__':
server('127.0.0.1',8080)
客户端:
from socket import *
client=socket(AF_INET,SOCK_STREAM)
client.connect(('127.0.0.1',8080))
while True:
msg=input('>>: ').strip()
if not msg:continue
client.send(msg.encode('utf-8'))
msg=client.recv(1024)
print(msg.decode('utf-8'))
多线程并发多个客户端:
from threading import Thread
from socket import *
import threading
def client(server_ip,port):
c=socket(AF_INET,SOCK_STREAM) #套接字对象一定要加到函数内,即局部名称空间内,放在函数外则被所有线程共享,则大家公用一个套接字对象,那么客户端端口永远一样了
c.connect((server_ip,port))
count=0
while True:
c.send(('%s say hello %s' %(threading.current_thread().getName(),count)).encode('utf-8'))
msg=c.recv(1024)
print(msg.decode('utf-8'))
count+=1
if __name__ == '__main__':
for i in range(500):
t=Thread(target=client,args=('127.0.0.1',8080))
t.start()
标签:__,spawn,编程,并发,Gevent,gevent,time,print,import
From: https://www.cnblogs.com/xiao01/p/18049749