Created
July 26, 2016 02:36
-
-
Save JustinSDK/1baf2656b83322847005c4d70549cd9d to your computer and use it in GitHub Desktop.
producer_consumer
This file contains hidden or 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 sys | |
import random | |
def producer(): | |
while True: | |
data = random.randint(0, 9) | |
print('生產了:', data) | |
yield data | |
def consumer(): | |
while True: | |
data = yield | |
print('消費了:', data) | |
def clerk(jobs, producer, consumer): | |
print('執行 {} 次生產與消費'.format(jobs)) | |
p = producer() | |
c = consumer() | |
next(c) | |
for i in range(jobs): | |
data = next(p) | |
c.send(data) | |
clerk(int(sys.argv[1]), producer, consumer) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment