Python多任务-协程发展过程 yield 与 yield from 的用法

python多任务-协程
同步 异步

同步-是指代码调用IO操作时 必须等待IO操作完成才返回调用方式 同步是串行
异步-是指代码调用IO操作时 不必等IO操作完成就返回的调用方式 异步是并行

阻塞 非阻塞
阻塞:从调用者的角度出发 如果在调用的时候被卡住 不能再继续向下运行 需要再等待 就说是阻塞
非阻塞:从调用都有角度出发 如果在调用的时候 凤有被卡住 能够继续向下运行 无需等待 不说是非阻塞
代码中 input等待输入是阻塞 print 是非阻塞
生成器-send方法
send方法有一个参数 该参数指定的是上一次被挂起的yield语句的返回值
使用yield完成多任务

# 斐波拉契数列  0 1,1,2,3,5,8,13,21,
# 除了next 来启动生成器 还有其他什么方法可以? 可通知send方法
def create_num(num):
    a,b = 0,1
    current_num = 0

    while current_num < num:
        result = yield a
        print('result-->',result)
        a,b = b, a+b
        current_num += 1

    # return 'ellen'    # 如何获取返回值? 不可用for循环也不可用next 在异常捕获中取得

g = create_num(5)
# for i in p:
#     print(i)

print(next(g))
# print(next(g))
# print(next(g))
# print(next(g))
# print(next(g))

# while True:
#     try:
#         ret = next(g)
#         print(ret)
#     except Exception as e:
#         print(e)
#         # print(e.value,'--')
#         break

# 报错 不能发送非空值启动生成器 必须先发送一个None
# print(g.send('ellen'))

# print(g.send(None))
print(next(g))
# 关闭生成器
# g.close()
# 如果前面不调用next 第一次必须发送None
print(g.send('ellen'))
print(g.send('ellen'))
print(g.send('ellen'))
# print(g.send('ellen'))

使用yield完成多任务消耗资源最小效率高


# import time
#
# def task1():
#     while True:
#         print('--1--')
#         time.sleep(0.1)
#         yield
#
#
# def task2():
#     while True:
#         print('--2--')
#         time.sleep(0.1)
#         yield
#
# def main():
#     t1 = task1()
#     t2 = task2()
#     while True:
#         next(t1)
#         next(t2)
#
#
# if __name__ == '__main__':
#         main()

yield from介绍

'''
python3 新加了yield from语法
'''
from itertools import chain

# lis = [1,2,3]
# dic = {
#     'name':'ellen',
#     'age':18
# }
# chain 相当于实现了3个for循环

# 返回一个对象<itertools.chain object at 0x00000000026A8748>
# 加list 强制转换后结果:[1, 2, 3, 'name', 'age', 5, 6, 7, 8, 9]
# print(list(chain(lis,dic,range(5,10))))

# *args,**kwargs 不定长参数
# def my_chain(*args,**kwargs):
#     for my_iterable in args:
#         # for value in my_iterable:
#         #     yield value
#         yield from my_iterable
#
#
# for value in my_chain(lis,dic,range(5,10)):
#     print(value)
#
# ---------------------------------------------
# lis = [1,2,3]
#
# def generator_1(lis):
#     yield lis
#
#
# def generator_2(lis):
#     yield from lis
#
#
# for i in generator_1(lis):
#     print(i)                     # [1,2,3]
#
# for i in generator_2(lis):
#     print(i)                        #  1 
                                         2
                                         3

‘’‘yield from 举例’’’


# 子生成器
def generator_1():
    total = 0
    while True:
        x = yield
        print('加',x)
        if not x:       # if not None:  True
            break
        total += x
    return total


# 委托生成器
def generator_2():
    while True:
        # 建立调用方和子生成器的通道
        total = yield from generator_1()  # 子生成器
        print('加和总数是:',total)

 # 调用方
def main():
    # g1 = generator_1()
    # g1.send(None)
    # g1.send(2)
    # g1.send(3)
    # g1.send(None)
    g2 = generator_2()
    g2.send(None)
    g2.send(2)
    g2.send(3)
    g2.send(None)
发布了106 篇原创文章 · 获赞 0 · 访问量 2367

猜你喜欢

转载自blog.csdn.net/weixin_45905671/article/details/105384595
今日推荐