Skip to content

Commit b491d69

Browse files
committed
add producer/consumer sample
1 parent e9d9ef6 commit b491d69

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

py3/advance/do_prod_cons.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
import time
5+
6+
def consumer():
7+
r = ''
8+
while True:
9+
n = yield r
10+
if not n:
11+
return
12+
print('[CONSUMER] Consuming %s...' % n)
13+
time.sleep(1)
14+
r = '200 OK'
15+
16+
def produce(c):
17+
c.send(None)
18+
n = 0
19+
while n < 5:
20+
n = n + 1
21+
print('[PRODUCER] Producing %s...' % n)
22+
r = c.send(n)
23+
print('[PRODUCER] Consumer return: %s' % r)
24+
c.close()
25+
26+
c = consumer()
27+
produce(c)

0 commit comments

Comments
 (0)