【python 生成器 面试必备】yield关键字,协程必知必会系列文章--自己控制程序调度,体验做上帝的感觉 1

news2024/10/6 21:23:43

python生成器系列文章目录

第一章 yield — Python (Part I)


文章目录

  • python生成器系列文章目录
  • 前言
  • 1. Generator Function 生成器函数
  • 2.并发和并行,抢占式和协作式
    • 2.Let’s implement Producer/Consumer pattern using subroutine:
  • 生成器的状态 generator’s states


前言

ref:https://medium.com/analytics-vidhya/yield-python-part-i-4dbfe914ad2d
这个老哥把yield讲清楚了,我来学习并且记录一下。


偶尔遇到Yield关键字时,它看起来相当神秘。这里,我们通过查看生成器如何使用yield获取值或将控制权返回给调用者来揭示yield所做的工作。我们也在看生成器generator的不同状态。让我们开始吧。

1. Generator Function 生成器函数

一个函数用了yield表达式后被称为生成器函数。

def happy_birthday_song(name='Eric'):
	yield "Happy Birthday to you"
	yield "Happy Birthday to you"
	yield f"Happy Birthday dear {name}"
	yield "Happy Birthday to you"
birthday_song_gen = happy_birthday_song() # generator creation
print(next(birthday_song_gen)) # prints first yield's value

birthday_song_gen 作为Generator被创建在第七行,相应的,生成器generator的执行通过调用next();
我们获得了yield的1个输出因为仅仅调用了一次next,接着generator是在suspend state(暂停/挂起状态),当另一个next()调用的时候,会激活执行并且返回第二个yield的值。像任何迭代器iterator一样,生成器将会exhausted 当stopIteration is encountered.

def happy_birthday_song(name='Eric'):
    yield "Happy Birthday to you"
    yield "Happy Birthday to you"
    yield f"Happy Birthday dear {name}"
    yield "Happy Birthday to you"
    
birthday_song_gen = happy_birthday_song() # generator creation
print(next(birthday_song_gen)) # prints first yield's value

# print rest of the yield's value
try:
    while True:
        print(next(birthday_song_gen))
except StopIteration:
    print('exhausted...')

2.并发和并行,抢占式和协作式

在这里插入图片描述
在这里插入图片描述
Cooperative multitasking is completely controlled by developer. Coroutine (Cooperative routine) is an example of cooperative multitasking.

Preemptive multitasking is not controlled by developer and have some sort of scheduler involved.

One of the ways to create coroutine in Python is generator.
在python中一种产生协程的做法是generator 生成器。

global 表示将变量声明为全局变量
nonlocal 表示将变量声明为外层变量(外层函数的局部变量,而且不能是全局变量)

def average():
	count = 0
	sum = 0
	def inner(value):
		nonlocal count
		nonlocal sum
		count += 1
		sum += value
		return sum/count
	return inner

def running_average(iterable):
	avg = average()
	for value in iterable:
		running_average = avg(value):
		print(running_average)
iterable = [1,2,3,4,5]
running_average(iterable)

输出:
在这里插入图片描述

The program control flow looks like this:
这个图要好好理解一下:
在这里插入图片描述

2.Let’s implement Producer/Consumer pattern using subroutine:

from collections import deque

def produce_element(dq, n):
    print('\nIn producer ...\n')
    for i in range(n):
        dq.appendleft(i)
        print(f'appended {i}')
        # if deque is full, return the control back to `coordinator`
        if len(dq) == dq.maxlen:
            yield

def consume_element(dq):
    print('\nIn consumer...\n')
    while True:
        while len(dq) > 0:
            item = dq.pop()
            print(f'popped {item}')
        # once deque is empty, return the control back to `coordinator`
        yield

def coordinator():
    dq = deque(maxlen=2)
    # instantiate producer and consumer generator
    producer = produce_element(dq, 5)
    consumer = consume_element(dq)

    while True:
        try:
            # producer fills deque
            print('next producer...')
            next(producer)
        except StopIteration:
            break
        finally:
            # consumer empties deque
            print('next consumer...')
            next(consumer)
            
if __name__ == '__main__':
    coordinator() 

output looks like this:

C:\Users\HP\.conda\envs\torch1.8\python.exe "C:\Program Files\JetBrains\PyCharm 2021.1.3\plugins\python\helpers\pydev\pydevd.py" --multiproc --qt-support=auto --client 127.0.0.1 --port 59586 --file D:/code/python_project/01-coroutine-py-mooc/8/demo_ccc.py
Connected to pydev debugger (build 211.7628.24)
next producer...

 In producer..

next consumer ...

 In consumer... 

popped 0
popped 1
next producer...
next consumer ...
popped 2
popped 3
next producer...
next consumer ...

Process finished with exit code -1

过程解析:
生产2个,消费2个,再生产两个,再消费两个,再生产一个,触发StopIteration,再转向finall 消费1个 整个进程结束。
详细的看英语:
What’s happening? Well, the following thing is happening:

  1. create a limited size deque , here size of 2

  2. coordinator creates an instance of producer generator and also mentioning how many elements it want to generate

  3. coordinator creates an instance of consumer generator

  4. producer runs until deque is filled and yields control back to caller

  5. consumer runs until deque is empty and yields control back to caller

Steps 3 and 4 are repeated until all elements the producer wanted to produce is complete. This coordination of consumer and producer is possible due to we being able to control state of a control flow.

生成器的状态 generator’s states

    from inspect import getgeneratorstate


    def gen(flowers):
        for flower in flowers:
            print(f'Inside loop:{getgeneratorstate(flower_gen)}')
            yield flower


    flower_gen = gen(['azalea', 'Forsythia', 'violas'])
    print(f"After generator creation:{getgeneratorstate(flower_gen)}\n")

    print('getting 1st flower')
    print("--==", next(flower_gen))
    print(f'After getting first flower: {getgeneratorstate(flower_gen)}\n')

    print(f'Get all flowers: {list(flower_gen)}\n')

    print(f'After getting all flowers: {getgeneratorstate(flower_gen)}')

输出:

C:\Users\HP\.conda\envs\torch1.8\python.exe D:/code/python_project/01-coroutine-py-mooc/8/demo_ccc.py
After generator creation:GEN_CREATED

getting 1st flower
Inside loop:GEN_RUNNING
--== azalea
After getting first flower: GEN_SUSPENDED

Inside loop:GEN_RUNNING
Inside loop:GEN_RUNNING
Get all flowers: ['Forsythia', 'violas']

After getting all flowers: GEN_CLOSED

Process finished with exit code 0

We have a handy getgeneratorstate method from inspect module that gives state of a generator. From the output, we see there are four different states:

  1. GEN_CREATED
  2. GEN_RUNNING
  3. GEN_SUSPENDED
  4. GEN_CLOSED
    GEN_CREATED is a state when we instantiate a generator. GEN_RUNNING is a state when a generator is yielding value. GEN_SUSPENDED is a state when a generator has yielded value. GEN_CLOSED is a state when a generator is exhausted.

In summary, yield is used by generators to produce value or give control back to caller and generator has 4 states.

My next article will be sending values to generators!
下一篇文章介绍如何传值到生成器

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1208786.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【EI会议征稿】第四届环境资源与能源工程国际学术会议(ICEREE 2024)

第四届环境资源与能源工程国际学术会议(ICEREE 2024) 2024 4th International Conference on Environment Resources and Energy Engineering ICEREE(2020-2023)大会先后在珠海,昆明,长沙成功召开。会议主…

自定义Matplotlib中的颜色映射(cmap)

要自定义Matplotlib中的颜色映射(cmap),您可以按照以下步骤进行操作: 导入所需的库: import numpy as np import matplotlib.pyplot as plt from matplotlib.colors import LinearSegmentedColormap创建自定义颜色映…

ViewPager2和TabLayout协同使用,实现多Fragment页面切换类似于QQ音乐,bilibili效果

一、ViewPager2的基本用法 使用前先添加依赖: implementation androidx.appcompat:appcompat:1.4.0 // AndroidX AppCompatimplementation com.google.android.material:material:1.4.0 // Material Design Components1、制作Fragment 首先制作一个Fragment的xml布…

欧拉回路和欧拉路径

目录 欧拉回路基础 欧拉回路的定义 欧拉回路的性质 判断图中是否存在欧拉回路的java代码实现 寻找欧拉回路的三个算法 Hierholzer算法 详细思路 代码实现 欧拉路径 欧拉路径的定义 欧拉路径的性质 欧拉回路基础 欧拉回路的定义 欧拉回路遍历了所有的边,…

spring cloud 简介

springcloud 定义 1.定义:springcloud为开发人员提供了在分布式系统中快速构建一些通用模式的工具(例如配置管理、服务发现、断路器、路由、控制总线等)2.微服务:基于单体应用,基于业务进行拆分,每个服务都是独立应用…

栈的实现---超详细

栈的实现 文章目录 栈的实现一、栈的模型二、栈的代码实现以及测试用例①栈的初始化②入栈③出栈④弹出栈顶⑤判断栈空间是否为空⑥计算栈空间长度⑦销毁栈⑧测试用例 一、栈的模型 首先栈有两个概念 1.数据结构里的栈。2.语言/操作系统中的栈(内存空间),可能会在递…

xshell连接云服务器(保姆级教程)

文章目录 1. 前言2. 查看云服务器的信息3. xshell7连接云服务器 1. 前言 云服务器,也被称为Elastic Compute Service (ECS),是一种简单高效、安全可靠、处理能力可弹性伸缩的计算服务。它源于物理服务器集群资源池,可以像从大海中取水一样&am…

从流程优化到经营提效,法大大电子签全面助力智慧零售升级

在新零售模式下,“商业综合体、百货商场、连锁商超、连锁便利店、线上电商平台”等各类商业零售企业借助数字化的手段来改造和重塑传统零售流程和逻辑,实现全面数字化转型,包括线上线下一体化、全场景覆盖、全链条联通、全渠道经营、客户服务…

大数据安全 | 【实验】RSA加密解密

文章目录 📚关于RSA📚实验目的📚流程梳理🐇Step1:求解乘法逆元🐇Step2:生成密钥🐇Step3:加密解密🐇Step4:最后数据导入 📚实验结果 &a…

github使用手册

核心代码 配置用户名/邮箱 best practice git init #在本地初始化一个仓库 git add . #将当前目录所有的文件加入&#xff08;注意这里是加入&#xff09;到缓存区 git commit -m "xxx" #将当前缓存区里的内容提交到本地仓库 git remote add <remote_rep_name&g…

开启学习新时代,电大搜题助您实现梦想!

亲爱的读者朋友们&#xff0c;当您拥有追求知识的渴望&#xff0c;秉持着对成功的执着追求时&#xff0c;浙江开放大学&#xff08;广播电视大学&#xff09;诞生了——它向您开启了一扇通向知识殿堂的大门。而今&#xff0c;我们荣幸地向您推荐一款既简便又高效的学习利器——…

Android设计模式--原型模式

一&#xff0c;定义 原型模式就是用原型实例指定创建对象的种类&#xff0c;并通过拷贝这些原型创建新的对象 也就是说用户从一个实例中复制出一个内部属性一致的对象&#xff0c;这个被复制的对象就是原型。 原型模式多用于创建复杂的或者构造耗时的实例&#xff0c;因为这…

YOLO目标检测——苹果数据集下载分享【含对应voc、coco和yolo三种格式标签】

实际项目应用&#xff1a;监测果园中苹果的生长情况、水果品质监控、自动化分拣数据集说明&#xff1a;苹果检测数据集&#xff0c;真实场景的高质量图片数据&#xff0c;数据场景丰富标签说明&#xff1a;使用lableimg标注软件标注&#xff0c;标注框质量高&#xff0c;含voc(…

淘宝京东优惠券信息API接口系列

获取淘宝优惠券信息接口需要使用淘宝开放平台提供的API接口。以下是获取优惠券信息的步骤&#xff1a; 进入淘宝开放平台&#xff0c;注册并登录账号。在开放平台页面中&#xff0c;找到“优惠券”或“营销工具”等相关的API接口&#xff0c;根据需要进行选择。根据接口文档&a…

计算机组成原理:大而快——层次化存储

原文链接www.xiaocr.fun/index.php/2023/11/14/计算机组成原理大而快-层次化存储/ 引言 关于两种局部性 时间局部性&#xff1a;如果某个数据被访问&#xff0c;那么在不久的将来它可能再次被访问空间局部性&#xff1a;如果某个数据项被访问&#xff0c;与它相邻的数据项可…

接口自动化测试(Python+Requests+Unittest)合集详解教程

&#xff08;1&#xff09;接口自动化测试的优缺点 优点&#xff1a; 测试复用性。维护成本相对UI自动化低一些。回归方便。可以运行更多更繁琐的测试。 自动化的一个明显的好处是可以在较少的时间内运行更多的测试。为什么UI自动化维护成本更高&#xff1f; 因为前端页面变…

工作记录-------MySql主从同步

MySql主从同步简述&#xff1a; MySQL主从同步&#xff0c;可以实现将数据从一台数据库服务器同步到多台数据库服务器。MySQL数据库自带主从同步功能&#xff0c;经过配置&#xff0c;可以实现基于库、表结构的多种方案的主从同步。 Redis是一种高性能的内存数据库&#xff1…

【Hello Go】初识Go语言

初识Go语言 Go语言介绍Go语言是什么Go语言优势Go语言能用来做什么 Go语言环境安装第一个GO语言程序运行Go语言程序 Go语言介绍 Go语言是什么 go语言是是Google开发的一种静态强类型、编译型、并发型&#xff0c;并具有垃圾回收功能的编程语言. 静态类型&#xff1a;在静态类型…

【左程云算法全讲10】打表技巧和矩阵处理技巧

系列综述&#xff1a; &#x1f49e;目的&#xff1a;本系列是个人整理为了秋招面试的&#xff0c;整理期间苛求每个知识点&#xff0c;平衡理解简易度与深入程度。 &#x1f970;来源&#xff1a;材料主要源于左程云算法课程进行的&#xff0c;每个知识点的修正和深入主要参考…

算法笔记-其他高效的技巧与算法(未处理完)

算法笔记-其他高效的技巧与算法 前缀和 前缀和 #include <cstdio> #include <vector> using namespace std; const int MAXN 10000; int n, a[MAXN]; int sum[MAXN] { 0 };int main() {scanf("%d", &n);for (int i 0; i < n; i) {scanf("…