my task is improving efficiency of making rpc call. I believe I can use producer and consumer example. producer will get response, whereas consumer will write the received object to file.
I write a sample program for producer & consumer problem. It looks good. However, my question is that how does consumer know when to exit ?
here is my code:
5 is_exit = False
6 product = []
7
8 def consumer(cond):
9 global product
10 global is_exit
11
12 while True:
13 cond.acquire()
14 while len(product) == 0:
15 cond.wait()
16 product = product[1:]
17 print 'consume one product'
18 cond.release()
19
20 def producer(cond):
21 global product
22 global is_exit
23
24 for i in range(10):
25 cond.acquire()
26 print 'produce one good'
27 product.append(199)
28 cond.notify()
29 cond.release()
/// I use .join() to wait all threads to finish.
since producer will only produce 10 goods, there must be some way to notify consumer that producer will not produce anything and you should exit.
any advises will be helpful. Thanks a lot.
PS. what I mean 'gracefully' is that I want to consumer will not do any extra work as soon as producer tells him that there is no more goods to be produced.