安装gevent:
pychrm安装:
file -- settings -- Project: pythonProject -- Python Interpreter -- + -- 搜索gevent -- Install Package

#Greenlet
#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author: smoke
# file: gevent_xiecheng
# time: 2021/05/27
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)
print(gr1) ##<greenlet.greenlet object at 0x000000000212DB90>
# gr2 = greenlet(test2)
# gr1.switch() #用来完成greenlet对象切换
/home/smoke/文档/DocumentFile/PycharmProjects/选课系统Demo/Jaime/venv/bin/python /home/smoke/文档/DocumentFile/PycharmProjects/pythonProject/join/gevent_xiecheng.py
<greenlet.greenlet object at 0x7f7379ae33b0 (otid=0x7f7379ba4280) pending>
Process finished with exit code 0
#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author: smoke
# file: gevent_xiecheng
# time: 2021/05/27
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)
# print(gr1) ##<greenlet.greenlet object at 0x000000000212DB90>
gr2 = greenlet(test2)
# gr1.switch() #用来完成greenlet对象切换
/home/smoke/文档/DocumentFile/PycharmProjects/选课系统Demo/Jaime/venv/bin/python /home/smoke/文档/DocumentFile/PycharmProjects/pythonProject/join/gevent_xiecheng.py
Process finished with exit code 0
#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author: smoke
# file: gevent_xiecheng
# time: 2021/05/27
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)
# print(gr1) ##<greenlet.greenlet object at 0x000000000212DB90>
gr2 = greenlet(test2)
gr1.switch() #用来完成greenlet对象切换
/home/smoke/文档/DocumentFile/PycharmProjects/选课系统Demo/Jaime/venv/bin/python /home/smoke/文档/DocumentFile/PycharmProjects/pythonProject/join/gevent_xiecheng.py
12
56
34
78
Process finished with exit code 0
#Gevent
#Gevent是一个第三方库,可以轻松通过gevent实行并发同步或异步编程,在gevent中用到的主要模式是Greenlet,它是以C扩展模块形式接入Python的轻量级协程,Greenlet全部运行在主程序操作系统进程的内部,但它们协作式地调度。
#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author: smoke
# file: gevent_xiecheng
# time: 2021/05/27
import gevent
import time
def foo():
print('Running in foo',time.ctime())
gevent.sleep(1) #模拟i/o阻塞,不能使用time.sleep是cpu切换,现在是一个线程切换。
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),
])
/home/smoke/文档/DocumentFile/PycharmProjects/选课系统Demo/Jaime/venv/bin/python /home/smoke/文档/DocumentFile/PycharmProjects/pythonProject/join/gevent_xiecheng.py
Running in foo Thu May 27 22:01:42 2021
Explicit context to bar Thu May 27 22:01:42 2021
Explicit context switch to foo again Thu May 27 22:01:43 2021
Implicit context switch back to bar Thu May 27 22:01:44 2021
Process finished with exit code 0
#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author: smoke
# file: gevent_xiecheng
# time: 2021/05/27
import gevent
import time
def foo():
print('Running in foo',time.ctime())
time.sleep(1) #模拟i/o阻塞,不能使用time.sleep是cpu切换,现在是一个线程切换。
print('Explicit context switch to foo again',time.ctime())
def bar():
print('Explicit context to bar',time.ctime())
time.sleep(2)
print('Implicit context switch back to bar',time.ctime())
gevent.joinall([
gevent.spawn(foo),
gevent.spawn(bar),
])
/home/smoke/文档/DocumentFile/PycharmProjects/选课系统Demo/Jaime/venv/bin/python /home/smoke/文档/DocumentFile/PycharmProjects/pythonProject/join/gevent_xiecheng.py
Running in foo Thu May 27 22:04:37 2021
Explicit context switch to foo again Thu May 27 22:04:38 2021
Explicit context to bar Thu May 27 22:04:38 2021
Implicit context switch back to bar Thu May 27 22:04:40 2021
Process finished with exit code 0
#爬虫
#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author: smoke
# file: gevent_xiecheng
# time: 2021/05/27
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()
with open('xiaohua.html','wb') as f:
f.write(data)
print('%d bytes received from %s.' % (len(data),url))
f('http://www.xiaohuar.com')
/home/smoke/文档/DocumentFile/PycharmProjects/选课系统Demo/Jaime/venv/bin/python /home/smoke/文档/DocumentFile/PycharmProjects/pythonProject/join/gevent_xiecheng.py
GET: http://www.xiaohuar.com
24338 bytes received from http://www.xiaohuar.com.
Process finished with exit code 0
查看xiaohua.html文件
smoke@smoke-GS70-2PC-Stealth:~$ cat 文档/DocumentFile/PycharmProjects/pythonProject/join/xiaohua.html | more
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>中国大学生校花网-清纯校花美女图片大全</title>
点击谷歌浏览器按钮可以打开爬下来的页面

通过IO组在时会自动切换任务
#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author: smoke
# file: gevent_xiecheng
# time: 2021/05/27
from gevent import monkey;
#monkey.patch_all()
import gevent
from urllib.request import urlopen
import time
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.taobao.com/','https://www.baidu.com/','https://www.qq.com/']
start=time.time()
for url in l:
f(url)
print(time.time()-start)
/home/smoke/文档/DocumentFile/PycharmProjects/选课系统Demo/Jaime/venv/bin/python /home/smoke/文档/DocumentFile/PycharmProjects/pythonProject/join/gevent_xiecheng.py
GET: https://www.taobao.com/
110268 bytes received from https://www.taobao.com/.
GET: https://www.baidu.com/
227 bytes received from https://www.baidu.com/.
GET: https://www.qq.com/
22125 bytes received from https://www.qq.com/.
5.599690675735474
Process finished with exit code 0
#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author: smoke
# file: gevent_xiecheng
# time: 2021/05/27
from gevent import monkey;
#monkey.patch_all()
import gevent
from urllib.request import urlopen
import time
def f(url):
print('GET: %s' % url)
resp = urlopen(url)
data = resp.read()
print('%d bytes received from %s.' % (len(data),url))
start=time.time()
gevent.joinall([
gevent.spawn(f,'https://www.taobao.com/'),
gevent.spawn(f,'https://www.baidu.com/'),
gevent.spawn(f,'https://www.qq.com/')
])
print(time.time()-start)
/home/smoke/文档/DocumentFile/PycharmProjects/选课系统Demo/Jaime/venv/bin/python /home/smoke/文档/DocumentFile/PycharmProjects/pythonProject/join/gevent_xiecheng.py
GET: https://www.taobao.com/
110200 bytes received from https://www.taobao.com/.
GET: https://www.baidu.com/
227 bytes received from https://www.baidu.com/.
GET: https://www.qq.com/
22125 bytes received from https://www.qq.com/.
0.5885026454925537
Process finished with exit code 0
#!/usr/bin/env python3.8
# -*- coding: UTF-8 -*-
# __author: smoke
# file: gevent_xiecheng
# time: 2021/05/27
from gevent import monkey;
monkey.patch_all() #补丁,最大程度监听i/o阻塞
import gevent
from urllib.request import urlopen
import time
def f(url):
print('GET: %s' % url)
resp = urlopen(url)
data = resp.read()
print('%d bytes received from %s.' % (len(data),url))
start = time.time()
gevent.joinall([
gevent.spawn(f,'https://www.taobao.com/'),
gevent.spawn(f,'https://www.baidu.com/'),
gevent.spawn(f,'https://www.qq.com/')
])
print(time.time()-start)
/home/smoke/文档/DocumentFile/PycharmProjects/选课系统Demo/Jaime/venv/bin/python /home/smoke/文档/DocumentFile/PycharmProjects/pythonProject/join/gevent_xiecheng.py
GET: https://www.taobao.com/
GET: https://www.baidu.com/
GET: https://www.qq.com/
227 bytes received from https://www.baidu.com/.
110200 bytes received from https://www.taobao.com/.
22125 bytes received from https://www.qq.com/.
0.22603464126586914
Process finished with exit code 0
浙公网安备 33010602011771号