Skip to content

Instantly share code, notes, and snippets.

@c1ay
Created August 16, 2017 06:32
Show Gist options
  • Save c1ay/6418eeefb81e7c665f013bdc4b447fe0 to your computer and use it in GitHub Desktop.
Save c1ay/6418eeefb81e7c665f013bdc4b447fe0 to your computer and use it in GitHub Desktop.
用yield单线程实现生产者,消费者模型.
import time
def consumer():
r = ''
while True:
n = yield r
if not n:
return
print('[CONSUMER] Consuming %s...' % n)
time.sleep(1)
r = '200 OK'
def producer(c):
next(c)
for i in range(1, 10):
print('[PRODUCER] Producing %s...' % i)
r = c.send(i)
print('[PRODUCER] Consumer return: %s ' % r)
c.close()
if __name__ == '__main__':
c = consumer()
producer(c)
@c1ay
Copy link
Author

c1ay commented Aug 16, 2017

深度截图20170816143257.png

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment