生成器实现多任务

from queue import Queue

q = Queue()


def func1():
while not q.empty():
i = q.get()
print(f'------func1------{i}')
yield i


def func2():
while not q.empty():
i = q.get()
print(f'------func2------{i}')
yield i


for i in range(10):
q.put(i)

f1 = func1()
f2 = func2()
while True:
try:
print(next(f1))
print(next(f2))
except StopIteration:
break
posted @ 2022-04-24 16:36  狒狒桑  阅读(25)  评论(0编辑  收藏  举报