[basic] add yield

This commit is contained in:
vamoosebbf 2021-03-06 14:22:05 +08:00
parent d304dc8a2e
commit 1179694d88
2 changed files with 101 additions and 0 deletions

39
basic/demo_yield.py Normal file
View File

@ -0,0 +1,39 @@
def event():
print('start yield')
# return next(task) and yield next(task.send('set_two'))
one = yield 'get_one'
assert(one == 'set_two')
print(one)
yield 'get_two' # return next(task) and yield next(task.send('set_two'))
print('exit yield')
yield # yield next() to exit or raise StopIteration
task = event()
run_one = next(task) # need next(task) init and next(task) == task.send(None)
# so next(task) => yield 'get_one' => run_one = 'get_one'
assert(run_one == 'get_one')
run_two = task.send('set_two')
assert(run_two == 'get_two')
print('run : ', run_one, ' and ', run_two)
try:
next(task)
print('run end')
next(task) # will raise StopIteration
except Exception as e:
print('yield StopIteration')
if __name__ == '__main__':
def task():
while True:
print('hello')
yield
tmp = task()
while True:
next(tmp)
while True:
print('hello')

62
basic/demo_yield_task.py Normal file
View File

@ -0,0 +1,62 @@
class Task:
def __init__(self, event=(lambda: print('task running'))):
self.event = event
self.cb = self.pre()
next(self.cb)
print('task init')
def pre(self):
print('task start')
flag = True
while flag is True:
flag = yield flag
self.event()
print('task exit')
def run(self, flag=True):
try:
res = self.cb.send(flag)
return res
except StopIteration as e:
return False
if __name__ == "__main__":
tmp = Task()
assert(tmp.run())
assert(tmp.run())
assert(False == tmp.run(False))
print(tmp.run())
print(tmp.run(False))
class music:
def __init__(self):
self.task = Task(self.pre)
self.args = None
print('music init')
def pre(self):
print('pre ', self.args)
def play(self, size=10):
self.args = list(range(size))
print(self.task.run())
def stop(self):
print(self.task.run(False))
self.__init__()
def loop(self):
self.alive = False
while self.alive:
self.play()
stop()
tmp = music()
tmp.play()
tmp.stop()
tmp.play()