We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent a1269f1 commit efc2df8Copy full SHA for efc2df8
docs/03_链表/yild.md
@@ -151,4 +151,29 @@ producer(consumer())
151
整个流程无锁,由一个线程执行,produce和consumer协作完成任务,所以称为“协程”,
152
而非线程的抢占式多任务。
153
"""
154
+```
155
+```py
156
+# -*- coding:utf-8 -*-
157
+import time
158
+def consumer():
159
+ r = '342'
160
+ while True:
161
+ n = yield r
162
+ if not n:
163
+ return
164
+ print('[CONSUMER] Consuming %s...' % n)
165
+ time.sleep(1)
166
+ r = '200 OK'
167
+def produce(c):
168
+ print '## ',c.next()
169
+ n = 0
170
+ while n < 5:
171
+ n = n + 1
172
+ print('[PRODUCER] Producing %s...' % n)
173
+ r = c.send(n)
174
+ print('[PRODUCER] Consumer return: %s' % r)
175
+ c.close()
176
+if __name__=='__main__':
177
+ c = consumer()
178
+ produce(c)
179
```
0 commit comments