风-fmgao

导航

队列queue

#!/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time    : 2018/6/19 9:11
# @File    : 线程queue.py


import queue

# q = queue.Queue(3)  # 先进先出- 队列
#
# q.put('first')
# q.put(2)
# q.put('third')
# # q.put(4)
# # 第四个放不进去是因为put有一个默认参数block=True阻塞,修改为False报错queue.full
# # timeout=3 超过3秒也是报错
#
#
# print(q.get())
# print(q.get())
# print(q.get())
# # print(q.get(block=True, timeout=3))
# # print(q.get(block=False, ))  # q.get_nowait()

# q = queue.LifoQueue(3)  # 后进先出->堆栈
# q.put('first')
# q.put(2)
# q.put('third')
#
# print(q.get())
# print(q.get())
# print(q.get())

q = queue.PriorityQueue()  # 优先级队列,数字越小优先级越高
q.put((10, 'one'))
q.put((40, 'two'))
q.put((30, 'three'))

print(q.get())
print(q.get())
print(q.get())

# 结果
# (10, 'one')
# (30, 'three')
# (40, 'two')

posted on 2018-06-19 09:31  风-fmgao  阅读(107)  评论(0编辑  收藏  举报