队列的类型和常用方法
队列是一种数据结构,它类似于列表。但列表是线程不安全的,而队列是线程安全的。
python的queue(python3,python2为Queue)提供了3种队列:
- Queue:先进先出型(First In First Out)。
- LifoQueue:后进先出型(Last In First Out)。
- PriorityQueue:优先级型,为队列中的元素自定义优先级,按照优先级获取元素。
每种队列的构造方法都有一个maxsize的属性,默认为0,该属性用来指定队列存放的最大元素个数。如果为0或小于0,则队列是无限长的。
队列中的方法:
- q.put(10):将某个元素放到队列中。该方法中的参数item(即要存放的元素)是必须指定的;
- 参数block=True,该参数是否采取阻塞的方式向队列中放置元素。如果block=True,则put方法会在队列已经满了的情况(没有空余位置)下阻塞,直到队列中空出位置后再将元素放置到队列中;如果block=False,不论队列是否有空余位置都会尝试将元素放到队列中,当队列已满时会抛出“满队列异常(Full exception)”。q.put_nowait()方法相当于q.put(block=False)。
- 参数timeout=None,当该参数被设置为x(大于或等于0)时,put方法最多会阻塞x秒,之后尝试向队列中存放元素,如果队列已满,则会抛出Full exception。
- q.get():对于Queue创建的队列来说,该方法会从队列头部获取并删除一个元素(最先进入队列的元素)。该方法同样也有可选参数block=True。默认情况下(True),该方法会在队列为空时阻塞,直到有元素被放入到队列中;当block=False时,该方法不会阻塞,如果队列为空,则会抛出"空队列异常(Empty exception)“。q.get_nowait()方法相当于q.get(block=False)。
- q.task_done():在完成某项队列操作后向队列发送一个信号。
- 判断队列属性的方法有:
- q.qsize():返回队列的长度;
- q.empty():如果队列为空,返回True,否则返回False。
- q.full():如果队列满了,返回True,否则返回False。
示例1:先进先出型,使用多个线程从同一队列中取出元素
import threading, time, queue class MyThread(threading.Thread): def run(self): while True:
if q.qsize() > 0:
item = q.get() # 从队列中取出元素
print("%s::取出的元素---->%s at %s" % (self.name, item, time.ctime()))
time.sleep(2)
else:
print("队列已空,无法取出元素")
break if __name__ == '__main__': q = queue.Queue() for i in range(10):
q.put(i) # 将0-9的数字放到队列q中 threads = [] for i in range(3):
threads.append(MyThread()) for t in threads:
t.start() for t in threads:
t.join() print("main thread ending....")
打印结果如下:
Thread-1::取出的元素---->0 at Tue Mar 26 16:33:41 2019
Thread-2::取出的元素---->1 at Tue Mar 26 16:33:41 2019
Thread-3::取出的元素---->2 at Tue Mar 26 16:33:41 2019
Thread-2::取出的元素---->3 at Tue Mar 26 16:33:43 2019
Thread-1::取出的元素---->4 at Tue Mar 26 16:33:43 2019
Thread-3::取出的元素---->5 at Tue Mar 26 16:33:43 2019
Thread-3::取出的元素---->6 at Tue Mar 26 16:33:45 2019
Thread-1::取出的元素---->7 at Tue Mar 26 16:33:45 2019
Thread-2::取出的元素---->8 at Tue Mar 26 16:33:45 2019
Thread-2::取出的元素---->9 at Tue Mar 26 16:33:47 2019
队列已空,无法取出元素
队列已空,无法取出元素
队列已空,无法取出元素
main thread ending.... ***Repl Closed***
可以看到,3个线程循环的从队列中取出元素,元素获取的顺序与放入队列的顺序一致。
示例2:LiFoQueue(后进先出队列)与PriorityQueue(优先级队列)
import queue def my_LiFo_queue():
q = queue.LifoQueue()
for i in range(5):
q.put(i) while not q.empty():
print("LiFo queue -->", q.get()) def my_proirity_queue():
q = queue.PriorityQueue()
q.put(10)
q.put(7)
q.put(3)
q.put(1) while not q.empty():
print("proirity queue-->", q.get()) my_LiFo_queue() my_proirity_queue()
打印结果如下所示:
LiFo queue --> 4
LiFo queue --> 3
LiFo queue --> 2
LiFo queue --> 1
LiFo queue --> 0
proirity queue--> 1
proirity queue--> 3
proirity queue--> 7
proirity queue--> 10 Process finished with exit code 0
对于PriorityQueue,我们可以自定义它的内部比较方法,为每个元素指定优先级后get()方法就会根据优先级来获取队列中的元素。
import queue class Skill(object):
def __init__(self, priority, description):
self.priority = priority # 优先级参照
self.description = description def __lt__(self, other): # 重新定义实例间的比较规则(<)
return self.priority < other.priority def __str__(self): # 重新定义该类实例的打印信息
return "(%s, '%s')" % (self.priority, self.description) def PriorityQueue_class():
q = queue.PriorityQueue()
q.put(Skill(7, 'proficient7'))
q.put(Skill(5, 'proficient5'))
q.put(Skill(6, 'proficient6'))
q.put(Skill(10, 'expert'))
q.put(Skill(1, 'novice'))
print('end')
while not q.empty():
print(q.get()) PriorityQueue_class()
打印结果如下所示:
end
(1, 'novice')
(5, 'proficient5')
(6, 'proficient6')
(7, 'proficient7')
(10, 'expert') Process finished with exit code 0
可见,get方法是按照优先级从低到高的顺序依次取出元素的。
参考:
https://www.cnblogs.com/itogo/p/5635629.html
https://www.cnblogs.com/saolv/p/9502124.html