当前位置:网站首页>协程
协程
2022-07-25 11:23:00 【托塔天王李】
协程
协程,又叫做微线程,纤程。
子程序,或者叫函数,在所有的语言中都是层级的调用,比如A调B,B在执行的过程中又调用了C,C执行完毕返回,B执行完毕返回,最后A执行完毕。所以子程序的调用是通过栈实现的,一个线程就是执行一个子程序。
协程看上去也是子程序,但是在执行过程中,在子程序的内部是可以中断的,然后去执行别的子程序,在适当的时候再返回来接着执行。
注意:在一个子程序中中断,去执行其他的子程序,不是函数调用,有点类似CPU中断。
把生产者和消费者改成协程
import time
def consumer():
r = ''
while True:
n = yield r
if not n:
return
print('[CONSUMER] Consuming %s...' % n)
time.sleep(1)
r = '200 OK'
def produce(c):
c.next()
n = 0
while n < 5:
n = n + 1
print('[PRODUCER] Producing %s...' % n)
r = c.send(n)
print('[PRODUCER] Consumer return: %s' % r)
c.close()
if __name__=='__main__':
c = consumer()
produce(c)
协程优点
协程最大的优点就是协程极高的执行效率。因为子程序的切换不是线程切换,而是由程序自身控制,因此没有线程切换的开销,和多线程比,线程的数量越多,协程的性能越好。
协程的第二大优势就是不需要多线程的锁机制,因为只有一个线程,也不写变量冲突,在协程中控制共享资源不加锁,只需要判断状态就好了,所以执行效率比多线程高很多。
边栏推荐
- Brpc source code analysis (V) -- detailed explanation of basic resource pool
- LeetCode 50. Pow(x,n)
- Meta learning (meta learning and small sample learning)
- Introduction to redis
- Eureka注册中心开启密码认证-记录
- 【AI4Code】《CoSQA: 20,000+ Web Queries for Code Search and Question Answering》 ACL 2021
- Solutions to the failure of winddowns planning task execution bat to execute PHP files
- PHP uploads the FTP path file to the curl Base64 image on the Internet server
- 【黑马早报】运营23年,易趣网宣布关停;蔚来对大众CEO抛出橄榄枝;华为天才少年曾放弃360万年薪;尹烨回应饶毅炮轰其伪科学...
- 【GCN】《Adaptive Propagation Graph Convolutional Network》(TNNLS 2020)
猜你喜欢

【多模态】《TransRec: Learning Transferable Recommendation from Mixture-of-Modality Feedback》 Arxiv‘22

Learning to Pre-train Graph Neural Networks(图预训练与微调差异)

GPT plus money (OpenAI CLIP,DALL-E)

Hardware connection server TCP communication protocol gateway

和特朗普吃了顿饭后写下了这篇文章

【6篇文章串讲ScalableGNN】围绕WWW 2022 best paper《PaSca》

Go garbage collector Guide

Behind the screen projection charge: iqiyi's quarterly profit, is Youku in a hurry?

【AI4Code】CodeX:《Evaluating Large Language Models Trained on Code》(OpenAI)

Innovation and breakthrough! AsiaInfo technology helped a province of China Mobile complete the independent and controllable transformation of its core accounting database
随机推荐
【图攻防】《Backdoor Attacks to Graph Neural Networks 》(SACMAT‘21)
Scott+Scott律所计划对Yuga Labs提起集体诉讼,或将确认NFT是否属于证券产品
R语言使用ggpubr包的ggarrange函数将多幅图像组合起来、使用ggexport函数将可视化图像保存为jpeg格式(width参数指定宽度、height参数指定高度、res参数指定分辨率)
【高并发】SimpleDateFormat类到底为啥不是线程安全的?(附六种解决方案,建议收藏)
剑指 Offer 22. 链表中倒数第k个节点
R语言ggplot2可视化:使用ggpubr包的ggviolin函数可视化小提琴图、设置add参数在小提琴内部添加抖动数据点以及均值标准差竖线(jitter and mean_sd)
JS 面试题:手写节流(throttle)函数
PHP curl post x-www-form-urlencoded
【AI4Code】《CoSQA: 20,000+ Web Queries for Code Search and Question Answering》 ACL 2021
那些离开网易的年轻人
Those young people who left Netease
How to solve the problem of the error reported by the Flink SQL client when connecting to MySQL?
selenium使用———xpath和模拟输入和模拟点击协作
The applet image cannot display Base64 pictures. The solution is valid
30 sets of Chinese style ppt/ creative ppt templates
【AI4Code】《GraphCodeBERT: Pre-Training Code Representations With DataFlow》 ICLR 2021
Atomic atomic class
php curl post Length Required 错误设置header头
dirReader.readEntries 兼容性问题 。异常错误DOMException
Figure neural network for recommending system problems (imp-gcn, lr-gcn)