-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple_yield.py
68 lines (58 loc) · 1.58 KB
/
simple_yield.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Simple demo for coroutine by "yield".
"""
__author__ = 'Ziang Lu'
from typing import Coroutine
def consumer() -> Coroutine:
"""
Consumer coroutine.
:return: coroutine
"""
print('[CONSUMER] Starting consumer...')
r = ''
while True:
n = yield r # 1. 协程运行至第一个yield, 并挂起
# 2. 接收参数n, 继续执行
# 3. 生成结果r传出, 并挂起
# 4 -> 2
if not n:
return
print(f'[CONSUMER] Consuming {n}...')
r = '200 OK'
def produce(con: Coroutine) -> None:
"""
Producer coroutine.
:param con: coroutine
:return: None
"""
con.send(None) # 1. 启动协程
n = 1
while n <= 5:
print(f'[PRODUCER] Producing {n}...')
r = con.send(n) # 2. 通过send(n)将参数n传给协程, 并挂起当前执行, 等待协程执行结果
# 3. 接收协程传回的结果r
# 4 -> 2
print(f'[PRODUCER] Consumer return: {r}')
n += 1
con.close()
c = consumer()
produce(c)
# Output:
# [CONSUMER] Starting consumer...
# [PRODUCER] Producing 1...
# [CONSUMER] Consuming 1...
# [PRODUCER] Consumer return: 200 OK
# [PRODUCER] Producing 2...
# [CONSUMER] Consuming 2...
# [PRODUCER] Consumer return: 200 OK
# [PRODUCER] Producing 3...
# [CONSUMER] Consuming 3...
# [PRODUCER] Consumer return: 200 OK
# [PRODUCER] Producing 4...
# [CONSUMER] Consuming 4...
# [PRODUCER] Consumer return: 200 OK
# [PRODUCER] Producing 5...
# [CONSUMER] Consuming 5...
# [PRODUCER] Consumer return: 200 OK