Python-100-Days/Day01-15/code/Day13/coroutine2.py

43 lines
1.0 KiB
Python
Raw Normal View History

2018-04-27 00:00:22 +08:00
"""
使用协程 - 查看协程的状态
Version: 0.1
Author: 骆昊
Date: 2018-03-21
"""
from time import sleep
from inspect import getgeneratorstate
def build_deliver_man(man_id):
2019-05-03 21:17:36 +08:00
total = 0
while True:
total += 1
print('%d号快递员准备接今天的第%d单.' % (man_id, total))
pkg = yield
print('%d号快递员收到编号为%s的包裹.' % (man_id, pkg))
sleep(0.5)
2018-04-27 00:00:22 +08:00
def package_center(deliver_man, max_per_day):
2019-05-03 21:17:36 +08:00
num = 1
# 创建状态(GEN_CREATED) - 等待开始执行
print(getgeneratorstate(deliver_man))
deliver_man.send(None)
# 挂起状态(GEN_SUSPENDED) - 在yield表达式处暂停
print(getgeneratorstate(deliver_man))
# next(deliver_man)
while num <= max_per_day:
package_id = 'PKG-%d' % num
deliver_man.send(package_id)
num += 1
deliver_man.close()
# 结束状态(GEN_CLOSED) - 执行完毕
print(getgeneratorstate(deliver_man))
print('今天的包裹派送完毕!')
2018-04-27 00:00:22 +08:00
dm = build_deliver_man(1)
package_center(dm, 10)