forked from yidao620c/python3-cookbook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreadgenerate.py
More file actions
44 lines (33 loc) · 751 Bytes
/
Copy paththreadgenerate.py
File metadata and controls
44 lines (33 loc) · 751 Bytes
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
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
Topic: 协程与微线程
Desc :
"""
from collections import deque
__author__ = 'Xiong Neng'
def foo():
for n in range(5):
print('I\'m foo %d' % n)
yield
def bar():
for n in range(10):
print("I'm bar %d" % n)
yield
def spam():
for n in range(7):
print("I'm spam %d" % n)
def demo():
taskqueue = deque()
taskqueue.append(foo())
taskqueue.append(bar())
taskqueue.append(spam())
while taskqueue:
task = taskqueue.pop()
try:
task.__next__()
taskqueue.appendleft(task)
except (StopIteration, AttributeError):
pass
if __name__ == '__main__':
demo()