-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path05.py
More file actions
37 lines (34 loc) · 972 Bytes
/
05.py
File metadata and controls
37 lines (34 loc) · 972 Bytes
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
import threading
import time
import queue
class Producer(threading.Thread):
def run(self):
global queue
count = 0
while True:
if queue.qsize()<1000:
for i in range(100):
count = count + 1
msg = '生成产品' + str(count)
queue.put(msg)#放入消息
print(msg)
time.sleep(0.5)
class Consumer(threading.Thread):
def run(self):
global queue
while True:
if queue.qsize()> 100:
for i in range(3):
msg = self.name + '消费了' + queue.get()#拿出消息
print(msg)
time.sleep(1)
if __name__ == '__main__':
queue=queue.Queue()
for i in range(500):
queue.put("初始产品" + str(i))
for i in range(2):
p = Producer()
p.start()
for i in range(5):
c = Consumer()
c.start()