Python 携程
一、协程
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()
Python 携程的更多相关文章
- python 携程asyncio 实现高并发示例2
https://www.bilibili.com/video/BV1g7411k7MD?from=search&seid=13649975876676293013 import asyncio ...
- python 携程asyncio实现高并发示例1
import asyncio #携程(携程不是函数) async def print_hello(): while True: print("hello world") await ...
- Python线程,进程,携程,I/O同步,异步
只有本人能看懂的-Python线程,进程,携程,I/O同步,异步 举个栗子: 我想get三个url,先用普通的for循环 import requests from multiprocessing im ...
- python爬虫-携程-eleven参数
携程-eleven分析 一.eleven的位置 通过对旁边栈的分析,它是在另一个js文件中调用的.那个js文件是一个自调用的函数,所以我们可以直接copy下来,用浏览器执行看看 执行运行是会报错的,u ...
- python对比线程,进程,携程,异步,哪个快
目录概念介绍测试环境开始测试测试[单进程单线程]测试[多进程 并行]测试[多线程 并发]测试[协程 + 异步]结果对比绘图展示概念介绍首先简单介绍几个概念: 进程和线程进程就是一个程序在一个数据集上的 ...
- 进程、线程和携程的通俗解释【刘新宇Python】
通过下面这张图你就能看清楚了,进程.线程和携程的关系 进程: 多个进程是可以运行在多个CPU当中的,比如你的电脑是4核,可以同时并行运行四个进程,这是真正物理上的并行运行. 线程: 每个进程又可以 ...
- 我所了解的各公司使用的 Ceph 存储集群 (携程、乐视、有云、宝德云、联通等)
Ceph 作为软件定义存储的代表之一,最近几年其发展势头很猛,也出现了不少公司在测试和生产系统中使用 Ceph 的案例,尽管与此同时许多人对它的抱怨也一直存在.本文试着整理作者了解到的一些使用案例. ...
- Python 协程 61
什么是协程 协程,又称微线程,纤程.英文名Coroutine.一句话说明什么是线程:协程是一种用户态的轻量级线程. 协程的特点 协程拥有自己的寄存器上下文和栈.协程调度切换时,将寄存器上下文和栈保存到 ...
- 18 11 26 用多进程 多线程 携程 实现 http 服务器的创建
下面是一个 多进程 服务器的创建 import socket import re import multiprocessing def service_client(new_socket): &qu ...
随机推荐
- 利用表格分页显示数据的js组件bootstrap datatable的使用
前面展示了datatable的简单使用,还可以通过bootstrap结合datatable来使用,这样可以进一步美化datatable插件 <!DOCTYPE html> <html ...
- Python2018-列表的相关操作
列表中存放的数据是可以进行修改的,比如"增"."删"."改" .“查” "增"-----append, extend, ...
- WebSocket异步通讯,实时返回数据
第一种方式 // 服务端: //var listener = new HttpListener(); // listener.Prefixes.Add("http://*:8080/&quo ...
- Notepad++怎么增加整行删除快捷键?添加/取消区块注释?
1.有网友说 Notepad++ 自带删除行功能,用快捷键 Ctrl+L 就可以删除一行.的确,这个快捷键是可以删除一行文本,但确切的说,Ctrl+L 并不是仅仅删除了一行文本,而是 剪切 了一行文本 ...
- Question Of AI Model Training
1 模型训练基本步骤 准备原始数据,定义神经网络结构及前向传播算法 定义loss,选择反向传播优化算法 生成Session,在训练数据进行迭代训练,使loss到达最小 在测试集或者验证集上对准确率进行 ...
- light1236 素数打表,质因数分解
不知道为什么会错 /* 求出 lcm(i,j)==n 的对数, 分解质因数,p1^e1 * p2^e2 * p3^e3 那么 i,j中必定有一个数有e1个p1因子,另一个任意即可 那么最终的结果就是 ...
- mysql出现10060错误
报错内容如下 Can't connect to MySQL server (10060) 提示不能连接 mysql服务. 首先检查任务管理器 看看mysqld.exe进程有没有启动.如果启动了就强制结 ...
- 数组练习题A财务管理
第一次看全英文的题,还是有点不舒服的感觉,还是用了翻译器 Larry graduated this year and finally has a job. He's making a lot of m ...
- DapperHelper 帮助类
using System; using System.Collections.Generic; using System.Configuration; using System.Data; using ...
- Tomcat8 启动慢 Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [53,161] milliseconds
修改$JAVA_PATH/jre/lib/security/java.security文件 将 securerandom.source=file:/dev/random 修改为 securerando ...