Created
August 16, 2017 06:32
-
-
Save c1ay/6418eeefb81e7c665f013bdc4b447fe0 to your computer and use it in GitHub Desktop.
用yield单线程实现生产者,消费者模型.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Author
c1ay
commented
Aug 16, 2017
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment