lhl
首页
python
leetcode
产品思想
软件测试
博客 (opens new window)
github (opens new window)
首页
python
leetcode
产品思想
软件测试
博客 (opens new window)
github (opens new window)
  • python

    • Python 基础

    • Python 数据库

    • 面向对象

    • Python Web

    • Python 进阶

      • 协程
        • 协程
      • 装饰器
      • decorator
      • filter
      • 列表生成式
      • 进程&线程
      • 生成器
      • 匿名函数
      • map&&reduce
      • venv
  • leetcode

  • 软件测试

  • Git

  • linux

  • 产品

  • MySql

  • docker

  • python
  • 进阶
2023-07-22
目录

协程

# 协程

# 简单理解

## 消费者
def consumer():
    r = ''
    while True:
        n = yield r
        if not n:
            return
        print('[CONSUMER] Consuming %s...' % n)
        r = '200 ok'


## 生产者
def produce(c):
    c.send(None)
    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()


c = consumer()
produce(c)
  1. 整个过程只有一个进程,只有 produce 和 consumer 协作完成
  2. c.send(None), 开始协程
  3. n = yield r, 可以接收返回值,这个返回值n来自r = c.send(n), r,来自于 r = '200 ok'
  4. 建议 debug 一下,效果会清楚很多
flask数据库
装饰器

← flask数据库 装饰器→

最近更新
01
lhl learn notes
02
filter
06-09
03
decorator
06-09
更多文章>
Theme by Vdoing
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式