forked from zhanghe06/python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.py
73 lines (61 loc) · 2.21 KB
/
queue.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# encoding: utf-8
__author__ = 'zhanghe'
from Queue import Queue
import random
import threading
import time
class Producer(threading.Thread):
"""
Producer thread
"""
def __init__(self, t_name, queue):
threading.Thread.__init__(self, name=t_name)
self.data = queue
def run(self):
for i in range(5):
print "%s: %s is producing %d to the queue!" % (time.ctime(), self.getName(), i)
self.data.put(i)
time.sleep(random.randrange(10) / 5)
print "%s: %s finished!" % (time.ctime(), self.getName())
class Consumer(threading.Thread):
"""
Consumer thread
"""
def __init__(self, t_name, queue):
threading.Thread.__init__(self, name=t_name)
self.data = queue
def run(self):
for i in range(5):
val = self.data.get()
print "%s: %s is consuming. %d in the queue is consumed!" % (time.ctime(), self.getName(), val)
time.sleep(random.randrange(10))
print "%s: %s finished!" % (time.ctime(), self.getName())
def main():
"""
Main thread
:return:
"""
queue = Queue()
producer = Producer('Pro.', queue)
consumer = Consumer('Con.', queue)
producer.start()
consumer.start()
producer.join()
consumer.join()
print 'All threads terminate!'
if __name__ == '__main__':
main()
# 本程序是比较经典的生产者和消费者模型,运行结果:
# Fri Apr 24 00:05:42 2015: Pro. is producing 0 to the queue!
# Fri Apr 24 00:05:42 2015: Con. is consuming. 0 in the queue is consumed!
# Fri Apr 24 00:05:43 2015: Pro. is producing 1 to the queue!
# Fri Apr 24 00:05:43 2015: Con. is consuming. 1 in the queue is consumed!
# Fri Apr 24 00:05:44 2015: Pro. is producing 2 to the queue!
# Fri Apr 24 00:05:45 2015: Pro. is producing 3 to the queue!
# Fri Apr 24 00:05:46 2015: Pro. is producing 4 to the queue!
# Fri Apr 24 00:05:47 2015: Pro. finished!
# Fri Apr 24 00:05:51 2015: Con. is consuming. 2 in the queue is consumed!
# Fri Apr 24 00:05:53 2015: Con. is consuming. 3 in the queue is consumed!
# Fri Apr 24 00:05:53 2015: Con. is consuming. 4 in the queue is consumed!
# Fri Apr 24 00:06:02 2015: Con. finished!
# All threads terminate!