1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > python多线程队列两组数据_python 多线程2-队列同步

python多线程队列两组数据_python 多线程2-队列同步

时间:2021-12-27 19:27:26

相关推荐

python多线程队列两组数据_python 多线程2-队列同步

让我们考虑更复杂的一种场景:产品是各不相同的。这时只记录一个数量就不够了,还需要记录每个产品的细节。很容易想到需要用一个容器将这些产品记录下来。

Python的Queue模块中提供了同步的、线程安全的队列类,包括FIFO(先入先出)队列Queue,LIFO(后入先出)队列LifoQueue,和优先级队列PriorityQueue。这些队列都实现了锁原语,能够在多线程中直接使用。可以使用队列来实现线程间的同步。

用FIFO队列实现上述生产者与消费者问题的代码如下:

#encoding=utf-8

import threading

import time

from Queue import Queue

class Producer(threading.Thread):

def run(self):

global queue

count = 0

while True:

for i in range(100):

if queue.qsize() > 1000:

pass

else:

count = count +1

msg = '生成产品'+str(count)

queue.put(msg)

print msg

time.sleep(1)

class Consumer(threading.Thread):

def run(self):

global queue

while True:

for i in range(3):

if queue.qsize() < 100:

pass

else:

msg = self.name + '消费了 '+queue.get()

print msg

time.sleep(1)

queue = Queue()

def test():

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()

if __name__ == '__main__':

test()

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。