-
Notifications
You must be signed in to change notification settings - Fork 2
/
producter_consumer.py
43 lines (36 loc) · 973 Bytes
/
producter_consumer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env python2.7
from random import randint
from time import sleep
from Queue import Queue
from myThread import MyThread
def writeQueue(queue):
print "producing object for Q...",
queue.put("xxx", 1)
print "size now", queue.qsize()
def readQueue(queue):
val = queue.get(1)
print "consumed object from Q... size now", queue.qsize()
def writer(queue, loops):
for i in range(loops):
writeQueue(queue)
sleep(randint(1, 3))
def reader(queue, loops):
for i in range(loops):
readQueue(queue)
sleep(randint(2, 50))
funcs = [writer, reader]
nfuncs = range(len(funcs))
def main():
nloops = randint(2, 40)
q = Queue(32)
threads = []
for i in nfuncs:
t = MyThread(funcs[i], (q, nloops), funcs[i].__name__)
threads.append(t)
for i in nfuncs:
threads[i].start()
for i in nfuncs:
threads[i].join()
print "all DONE"
if __name__ == '__main__':
main()