当前位置:网站首页>协程
协程
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)
协程优点
协程最大的优点就是协程极高的执行效率。因为子程序的切换不是线程切换,而是由程序自身控制,因此没有线程切换的开销,和多线程比,线程的数量越多,协程的性能越好。
协程的第二大优势就是不需要多线程的锁机制,因为只有一个线程,也不写变量冲突,在协程中控制共享资源不加锁,只需要判断状态就好了,所以执行效率比多线程高很多。
边栏推荐
- Meta learning (meta learning and small sample learning)
- Median (二分答案 + 二分查找)
- 【Debias】Model-Agnostic Counterfactual Reasoning for Eliminating Popularity Bias in RS(KDD‘21)
- 【AI4Code】CodeX:《Evaluating Large Language Models Trained on Code》(OpenAI)
- Pycharm connects to the remote server SSH -u reports an error: no such file or directory
- Start with the development of wechat official account
- Behind the screen projection charge: iqiyi's quarterly profit, is Youku in a hurry?
- Solved files' name is invalid or doors not exist (1205)
- winddows 计划任务执行bat 执行PHP文件 失败的解决办法
- Atomic 原子类
猜你喜欢

LeetCode 50. Pow(x,n)

Heterogeneous graph neural network for recommendation system problems (ackrec, hfgn)

剑指 Offer 22. 链表中倒数第k个节点

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

Transformer variants (spark transformer, longformer, switch transformer)

客户端开放下载, 欢迎尝鲜

【AI4Code最终章】AlphaCode:《Competition-Level Code Generation with AlphaCode》(DeepMind)

Go 垃圾回收器指南

【GCN-RS】Region or Global? A Principle for Negative Sampling in Graph-based Recommendation (TKDE‘22)

OSPF综合实验
随机推荐
R语言组间均值是否相同的成对比较:使用pairwise.t.test函数执行多个分组数据均值的两两成对假设检验
客户端开放下载, 欢迎尝鲜
【Debias】Model-Agnostic Counterfactual Reasoning for Eliminating Popularity Bias in RS(KDD‘21)
Transformer变体(Sparse Transformer,Longformer,Switch Transformer)
【AI4Code】《InferCode: Self-Supervised Learning of Code Representations by Predicting Subtrees》ICSE‘21
[dark horse morning post] eBay announced its shutdown after 23 years of operation; Wei Lai throws an olive branch to Volkswagen CEO; Huawei's talented youth once gave up their annual salary of 3.6 mil
投屏收费背后:爱奇艺季度盈利,优酷急了?
Word中的空白页,怎么也删不掉?如何操作?
【AI4Code】《Contrastive Code Representation Learning》 (EMNLP 2021)
【GCN-CTR】DC-GNN: Decoupled GNN for Improving and Accelerating Large-Scale E-commerce Retrieval WWW22
[untitled]
Application and innovation of low code technology in logistics management
A beautiful gift for girls from programmers, H5 cube, beautiful, exquisite, HD
浅谈低代码技术在物流管理中的应用与创新
Atomic atomic class
银行理财子公司蓄力布局A股;现金管理类理财产品整改加速
【高并发】我用10张图总结出了这份并发编程最佳学习路线!!(建议收藏)
【GCN-RS】Are Graph Augmentations Necessary? Simple Graph Contrastive Learning for RS (SIGIR‘22)
【GCN-RS】Are Graph Augmentations Necessary? Simple Graph Contrastive Learning for RS (SIGIR‘22)
小程序image 无法显示base64 图片 解决办法 有效