当前位置:网站首页>Synergetic process
Synergetic process
2022-07-25 12:24:00 【Tota King Li】
coroutines
coroutines , Also called microthreading , fibers .
Subroutines , Or functions , In all languages, there are hierarchical calls , such as A transfer B,B In the process of execution, it calls C,C Execution complete return ,B Execution complete return , Last A completion of enforcement . So the subroutine is called through the stack , A thread is to execute a subroutine .
It also looks like a subroutine , But during execution , It can be interrupted inside the subroutine , Then execute other subroutines , Come back at the right time and carry out .
Be careful : Interrupt in a subroutine , To execute other subroutines , It's not a function call , It's kind of similar CPU interrupt .
Change producers and consumers into cooperative processes
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)
Advantages of cooperation
The biggest advantage of cooperative process is its high execution efficiency . Because the switching of subroutines is not thread switching , It's controlled by the program itself , Therefore, there is no overhead of thread switching , Ratio to multithreading , The more threads , The better the performance of the coordination process .
The second advantage of coroutine is that it does not need multi-threaded locking mechanism , Because there's only one thread , Do not write variable conflicts , Control Shared resources without locking in the coroutine , You just have to judge the state , So execution efficiency is much higher than multithreading .
边栏推荐
- Week303 of leetcode (20220724)
- Zuul网关使用
- GPT plus money (OpenAI CLIP,DALL-E)
- Ecological profile of pytorch
- WPF project introduction 1 - Design and development of simple login page
- PyTorch的生态简介
- 【Flutter -- 实例】案例一:基础组件 & 布局组件综合实例
- 3.2.1 什么是机器学习?
- Zuul gateway use
- R language Visual scatter diagram, geom using ggrep package_ text_ The rep function avoids overlapping labels between data points (set the min.segment.length parameter to inf and do not add label segm
猜你喜欢

Hystrix使用

Eureka usage record

Intelligent information retrieval (overview of intelligent information retrieval)

Brpc source code analysis (II) -- the processing process of brpc receiving requests

通过Referer请求头实现防盗链

Jenkins配置流水线

RestTemplate与Ribbon简单使用

氢能创业大赛 | 国家能源局科技司副司长刘亚芳:构建高质量创新体系是我国氢能产业发展的核心

防范SYN洪泛攻击的方法 -- SYN cookie

scrapy 爬虫框架简介
随机推荐
Pairwise comparison of whether the mean values between R language groups are the same: pairwise hypothesis test of the mean values of multiple grouped data is performed using pairwise.t.test function
logstash
Build a series of vision transformer practices, and finally meet, Timm library!
RestTemplate与Ribbon简单使用
R language ggplot2 visualization: visualize the scatter diagram, add text labels to some data points in the scatter diagram, and use geom of ggrep package_ text_ The repl function avoids overlapping l
和特朗普吃了顿饭后写下了这篇文章
numpy初识
3.2.1 什么是机器学习?
1.1.1 欢迎来到机器学习
【4】 Layout view and layout toolbar usage
WPF project introduction 1 - Design and development of simple login page
mysql的表分区
记录一次线上死锁的定位分析
嵌套事务 UnexpectedRollbackException 分析与事务传播策略
Feign使用
【四】布局视图和布局工具条使用
2.1.2 机器学习的应用
MySQL练习二
selenium使用———安装、测试
论文解读(MaskGAE)《MaskGAE: Masked Graph Modeling Meets Graph Autoencoders》