当前位置: 首页 > news >正文

python基础-数据结构-leetcode刷题必看-queue---队列-python的底层构建

文章目录

  • 队列
    • 双端队列 deque
      • 底层存储
      • deque接口
        • 1. `__init__(self, iterable: Iterable[_T], maxlen: int | None = None) -> None`
        • 2. `append(self, __x: _T) -> None`
        • 3. `appendleft(self, __x: _T) -> None`
        • 4. `copy(self) -> Self`
        • 5. `count(self, __x: _T) -> int`
        • 6. `extend(self, __iterable: Iterable[_T]) -> None`
        • 7. `extendleft(self, __iterable: Iterable[_T]) -> None`
        • 8. `insert(self, __i: int, __x: _T) -> None`
        • 9. `index(self, __x: _T, __start: int = 0, __stop: int = ...) -> int`
        • 10. `pop(self) -> _T`
        • 11. `popleft(self) -> _T`
        • 12. `remove(self, __value: _T) -> None`
        • 13. `rotate(self, __n: int = 1) -> None`
    • 线程安全队列Queue
      • 属性
      • 方法
        • `__init__(self, maxsize: int = 0) -> None`
        • `_init(self, maxsize: int) -> None`
        • `empty(self) -> bool`
        • `full(self) -> bool`
        • `get(self, block: bool = True, timeout: float | None = None) -> _T`
        • `get_nowait(self) -> _T`
        • `_get(self) -> _T`
        • `put(self, item: _T, block: bool = True, timeout: float | None = None) -> None`
        • `put_nowait(self, item: _T) -> None`
        • `_put(self, item: _T) -> None`
        • `join(self) -> None`
        • `qsize(self) -> int`
        • `_qsize(self) -> int`
        • `task_done(self) -> None`
    • 线程安全优先级队列
    • 线程安全——栈
    • 线程安全的简单的队列——SimpleQueue

队列

在python有以下几种队列

  • deque 双端队列
  • Queue 队列 FIFO
  • LifoQueue 栈 LIFO
  • PriorityQueue 优先队列
  • SimpleQueue 简单队列 FIFO
    除了deque,其他都有一个共同的特点——线程安全
    在这里插入图片描述

双端队列 deque

由于python中的队列是基于双端队列来实现的,所以我们先查看deque的实现,如果你通过pycharm去追溯deque的源代码,你会发现里面只有类的定义并没有具体的实现,其代码如下:

class deque(MutableSequence[_T], Generic[_T]):@propertydef maxlen(self) -> int | None: ...@overloaddef __init__(self, *, maxlen: int | None = None) -> None: ...@overloaddef __init__(self, iterable: Iterable[_T], maxlen: int | None = None) -> None: ...def append(self, __x: _T) -> None: ...def appendleft(self, __x: _T) -> None: ...def copy(self) -> Self: ...def count(self, __x: _T) -> int: ...def extend(self, __iterable: Iterable[_T]) -> None: ...def extendleft(self, __iterable: Iterable[_T]) -> None: ...def insert(self, __i: int, __x: _T) -> None: ...def index(self, __x: _T, __start: int = 0, __stop: int = ...) -> int: ...def pop(self) -> _T: ...  # type: ignore[override]def popleft(self) -> _T: ...def remove(self, __value: _T) -> None: ...def rotate(self, __n: int = 1) -> None: ...def __copy__(self) -> Self: ...def __len__(self) -> int: ...# These methods of deque don't take slices, unlike MutableSequence, hence the type: ignoresdef __getitem__(self, __key: SupportsIndex) -> _T: ...  # type: ignore[override]def __setitem__(self, __key: SupportsIndex, __value: _T) -> None: ...  # type: ignore[override]def __delitem__(self, __key: SupportsIndex) -> None: ...  # type: ignore[override]def __contains__(self, __key: object) -> bool: ...def __reduce__(self) -> tuple[type[Self], tuple[()], None, Iterator[_T]]: ...def __iadd__(self, __value: Iterable[_T]) -> Self: ...def __add__(self, __value: Self) -> Self: ...def __mul__(self, __value: int) -> Self: ...def __imul__(self, __value: int) -> Self: ...def __lt__(self, __value: deque[_T]) -> bool: ...def __le__(self, __value: deque[_T]) -> bool: ...def __gt__(self, __value: deque[_T]) -> bool: ...def __ge__(self, __value: deque[_T]) -> bool: ...def __eq__(self, __value: object) -> bool: ...if sys.version_info >= (3, 9):def __class_getitem__(cls, __item: Any) -> GenericAlias: ...

底层存储

这说明其底层并不是由python写的,而是由效率更高的c语言的结构体实现,让我们大致浏览一下pythondeque的底层实现:
pythongihub仓库中,我门可以从cpythoncpython/Modules/_collectionsmodule.c)中找到deque的踪迹

#define BLOCKLEN 64
#define CENTER ((BLOCKLEN - 1) / 2)
#define MAXFREEBLOCKS 16typedef struct BLOCK {struct BLOCK *leftlink;PyObject *data[BLOCKLEN];struct BLOCK *rightlink;
} block;typedef struct {PyObject_VAR_HEADblock *leftblock;block *rightblock;Py_ssize_t leftindex;       /* 0 <= leftindex < BLOCKLEN */Py_ssize_t rightindex;      /* 0 <= rightindex < BLOCKLEN */size_t state;               /* incremented whenever the indices move */Py_ssize_t maxlen;          /* maxlen is -1 for unbounded deques */Py_ssize_t numfreeblocks;block *freeblocks[MAXFREEBLOCKS];PyObject *weakreflist;
} dequeobject;

如果学过数据结构的都知道,双端队列可以由动态数组双向列表实现

  • 如果使用数组实现双端队列,那么当从头结点删除数据时那么需要将后面的数据都要向前移动一格,在大批量数据的情况,从头结点删除的效率就很低,复杂度为O(n)
  • 如果使用双向链表实现双端队列,尽管能够解决动态数组从头结点效率低下的问题,但是算向链表会占用额外的空间、且随机访问的复杂度为 O ( n ) O(n) O(n),此外它具有链表的通病:缓存性能差(非连续存储空间,无法将整个存储块加载到内存,在内存中访问是跳跃式访问,不是连续访问,性能会差很多)

所以python底层选择了折中的方式,即使用链表保证在队头插入元素的高效性,也使用数组提高连续访问的高效能力,从上面的代码看,python的双端队列是一个双向链表块dequeobject,每个块block(BLOCK)是一个64大小的数组data,这样将其性能进行了折中。当然其插入删除的操作也变的更复杂。

deque接口

这些方法和构造函数都是Python标准库中collections.deque类的接口,用于操作双端队列。以下是对每个方法的详细介绍和用法示例:

1. __init__(self, iterable: Iterable[_T], maxlen: int | None = None) -> None

构造函数,用于初始化一个双端队列对象。

参数

  • iterable: 可选,初始化队列的可迭代对象。如果提供,该对象的元素会被按顺序添加到队列中。
  • maxlen: 可选,队列的最大长度。如果设置了最大长度,当队列满时,添加新元素会导致旧元素被丢弃。

示例

>>> from collections import deque
>>> deque()
deque([])
>>> deque([1,2,3])
deque([1, 2, 3])
>>> deque([1,2,3], maxlen=5)
deque([1, 2, 3], maxlen=5)
2. append(self, __x: _T) -> None

在队列的右端添加一个元素。

参数

  • __x: 要添加的元素。

示例

>>> dq = deque([1, 2, 3])
>>> dq.append(4)
>>> dq
deque([1, 2, 3, 4])
3. appendleft(self, __x: _T) -> None

在队列的左端添加一个元素。

参数

  • __x: 要添加的元素。

示例

>>> dq
deque([1, 2, 3, 4])
>>> dq.appendleft(0)
>>> dq
deque([0, 1, 2, 3, 4])
4. copy(self) -> Self

返回一个双端队列的浅拷贝。

示例

>>> dq.copy()
deque([0, 1, 2, 3, 4])
>>> dq.copy() == dq #判断相等的方法是元素相同
True
>>> dq_copy = dq.copy()
>>> dq_copy.append(5)
>>> dq_copy
deque([0, 1, 2, 3, 4, 5])
>>> dq
deque([0, 1, 2, 3, 4])
5. count(self, __x: _T) -> int

返回队列中指定元素的出现次数。

参数

  • __x: 要计数的元素。

示例

>>> dq
deque([0, 1, 2, 3, 4])
>>> dq.count(1)
1
6. extend(self, __iterable: Iterable[_T]) -> None

在队列的右端扩展一个可迭代对象的所有元素。

参数

  • __iterable: 包含要添加元素的可迭代对象。

示例

>>> dq.extend([3,4,5]) #接收可迭代类型
>>> dq
deque([0, 1, 2, 3, 4, 3, 4, 5])
7. extendleft(self, __iterable: Iterable[_T]) -> None

在队列的左端扩展一个可迭代对象的所有元素。注意,元素会按相反的顺序添加。

参数

  • __iterable: 包含要添加元素的可迭代对象。

示例

>>> dq
deque([0, 1, 2, 3, 4, 3, 4, 5])
>>> dq.extendleft((0,1,2)) # 并不是直接拼接在left,而是反向后拼接的
>>> dq
deque([2, 1, 0, 0, 1, 2, 3, 4, 3, 4, 5])
8. insert(self, __i: int, __x: _T) -> None

在指定位置插入一个元素。

参数

  • __i: 要插入的位置索引。如果索引超出范围,元素会被插入到队列的两端。
  • __x: 要插入的元素。

示例

>>> dq
deque([2, 1, 100, 0, 0, 1, 2, 3, 4, 3, 4, 5])
>>> dq.insert(20,100) # 超过的索引直接插在末尾
>>> dq
deque([2, 1, 100, 0, 0, 1, 2, 3, 4, 3, 4, 5, 100])
>>> dq.insert(-1,200) #支持负数索引
>>> dq
deque([2, 1, 100, 0, 0, 1, 2, 3, 4, 3, 4, 5, 200, 100])
>>> dq.insert(-20,50)
>>> dq
deque([50, 2, 1, 100, 0, 0, 1, 2, 3, 4, 3, 4, 5, 200, 100])
9. index(self, __x: _T, __start: int = 0, __stop: int = ...) -> int

返回指定值首次出现的索引。如果没有找到该值,将引发ValueError

参数

  • __x: 要查找的元素。
  • __start: 可选,搜索的起始位置。
  • __stop: 可选,搜索的结束位置。

示例

>>> dq.index(100)
3
>>> dq.index(100,start=5) # 非关键字参数
Traceback (most recent call last):File "<stdin>", line 1, in <module>
TypeError: index() takes no keyword arguments
>>> dq.index(100,5)
14
10. pop(self) -> _T

移除并返回队列右端的元素。如果队列为空,将引发IndexError

示例

>>> dq = deque([1, 2, 3])
>>> dq.pop()
3
>>> dq.pop()
2
>>> dq.pop()
1
>>> dq.pop()
Traceback (most recent call last):File "<stdin>", line 1, in <module>
IndexError: pop from an empty deque
>>>
11. popleft(self) -> _T

移除并返回队列左端的元素。如果队列为空,将引发IndexError

示例

>>> dq = deque([1, 2, 3])
>>> dq.popleft()
1
>>> dq.popleft()
2
>>> dq.popleft()
3
>>> dq.popleft()
Traceback (most recent call last):File "<stdin>", line 1, in <module>
IndexError: pop from an empty deque
>>>
12. remove(self, __value: _T) -> None

移除队列中第一个匹配的元素。如果没有找到该值,将引发ValueError

参数

  • __value: 要移除的元素。

示例

>>> dq = deque([1, 2, 3,4,3])
>>> dq.remove(3)
>>> dq
deque([1, 2, 4, 3])
>>> dq.remove(0)
Traceback (most recent call last):File "<stdin>", line 1, in <module>
ValueError: deque.remove(x): x not in deque
13. rotate(self, __n: int = 1) -> None

将队列旋转 n 步。如果 n 为正数,元素将从右端移到左端。如果 n 为负数,元素将从左端移到右端。

参数

  • __n: 旋转的步数。

示例

>>> dq = deque([1, 2, 3, 4])
>>> dq.rotate(2) #向右旋转
>>> dq
deque([3, 4, 1, 2])
>>> dq.rotate(-1) #向左旋转
>>> dq
deque([4, 1, 2, 3])

这些方法和接口提供了丰富的操作来管理和操作双端队列,适用于需要在两端进行频繁插入和删除操作的数据结构。

线程安全队列Queue

Queue的底层是基于deque实现的,因为Queue的功能是deque中的部分,只要对其进行限制其双端性遵循FIFO就可以使用。下面是Queue的官方代码

class Queue:'''Create a queue object with a given maximum size.If maxsize is <= 0, the queue size is infinite.'''def __init__(self, maxsize=0):self.maxsize = maxsizeself._init(maxsize)# mutex must be held whenever the queue is mutating.  All methods# that acquire mutex must release it before returning.  mutex# is shared between the three conditions, so acquiring and# releasing the conditions also acquires and releases mutex.self.mutex = threading.Lock()# Notify not_empty whenever an item is added to the queue; a# thread waiting to get is notified then.self.not_empty = threading.Condition(self.mutex)# Notify not_full whenever an item is removed from the queue;# a thread waiting to put is notified then.self.not_full = threading.Condition(self.mutex)# Notify all_tasks_done whenever the number of unfinished tasks# drops to zero; thread waiting to join() is notified to resumeself.all_tasks_done = threading.Condition(self.mutex)self.unfinished_tasks = 0def task_done(self):'''Indicate that a formerly enqueued task is complete.Used by Queue consumer threads.  For each get() used to fetch a task,a subsequent call to task_done() tells the queue that the processingon the task is complete.If a join() is currently blocking, it will resume when all itemshave been processed (meaning that a task_done() call was receivedfor every item that had been put() into the queue).Raises a ValueError if called more times than there were itemsplaced in the queue.'''with self.all_tasks_done:unfinished = self.unfinished_tasks - 1if unfinished <= 0:if unfinished < 0:raise ValueError('task_done() called too many times')self.all_tasks_done.notify_all()self.unfinished_tasks = unfinisheddef join(self):'''Blocks until all items in the Queue have been gotten and processed.The count of unfinished tasks goes up whenever an item is added to thequeue. The count goes down whenever a consumer thread calls task_done()to indicate the item was retrieved and all work on it is complete.When the count of unfinished tasks drops to zero, join() unblocks.'''with self.all_tasks_done:while self.unfinished_tasks:self.all_tasks_done.wait()def qsize(self):'''Return the approximate size of the queue (not reliable!).'''with self.mutex:return self._qsize()def empty(self):'''Return True if the queue is empty, False otherwise (not reliable!).This method is likely to be removed at some point.  Use qsize() == 0as a direct substitute, but be aware that either approach risks a racecondition where a queue can grow before the result of empty() orqsize() can be used.To create code that needs to wait for all queued tasks to becompleted, the preferred technique is to use the join() method.'''with self.mutex:return not self._qsize()def full(self):'''Return True if the queue is full, False otherwise (not reliable!).This method is likely to be removed at some point.  Use qsize() >= nas a direct substitute, but be aware that either approach risks a racecondition where a queue can shrink before the result of full() orqsize() can be used.'''with self.mutex:return 0 < self.maxsize <= self._qsize()def put(self, item, block=True, timeout=None):'''Put an item into the queue.If optional args 'block' is true and 'timeout' is None (the default),block if necessary until a free slot is available. If 'timeout' isa non-negative number, it blocks at most 'timeout' seconds and raisesthe Full exception if no free slot was available within that time.Otherwise ('block' is false), put an item on the queue if a free slotis immediately available, else raise the Full exception ('timeout'is ignored in that case).'''with self.not_full:if self.maxsize > 0:if not block:if self._qsize() >= self.maxsize:raise Fullelif timeout is None:while self._qsize() >= self.maxsize:self.not_full.wait()elif timeout < 0:raise ValueError("'timeout' must be a non-negative number")else:endtime = time() + timeoutwhile self._qsize() >= self.maxsize:remaining = endtime - time()if remaining <= 0.0:raise Fullself.not_full.wait(remaining)self._put(item)self.unfinished_tasks += 1self.not_empty.notify()def get(self, block=True, timeout=None):'''Remove and return an item from the queue.If optional args 'block' is true and 'timeout' is None (the default),block if necessary until an item is available. If 'timeout' isa non-negative number, it blocks at most 'timeout' seconds and raisesthe Empty exception if no item was available within that time.Otherwise ('block' is false), return an item if one is immediatelyavailable, else raise the Empty exception ('timeout' is ignoredin that case).'''with self.not_empty:if not block:if not self._qsize():raise Emptyelif timeout is None:while not self._qsize():self.not_empty.wait()elif timeout < 0:raise ValueError("'timeout' must be a non-negative number")else:endtime = time() + timeoutwhile not self._qsize():remaining = endtime - time()if remaining <= 0.0:raise Emptyself.not_empty.wait(remaining)item = self._get()self.not_full.notify()return itemdef put_nowait(self, item):'''Put an item into the queue without blocking.Only enqueue the item if a free slot is immediately available.Otherwise raise the Full exception.'''return self.put(item, block=False)def get_nowait(self):'''Remove and return an item from the queue without blocking.Only get an item if one is immediately available. Otherwiseraise the Empty exception.'''return self.get(block=False)# Override these methods to implement other queue organizations# (e.g. stack or priority queue).# These will only be called with appropriate locks held# Initialize the queue representationdef _init(self, maxsize):self.queue = deque()def _qsize(self):return len(self.queue)# Put a new item in the queuedef _put(self, item):self.queue.append(item)# Get an item from the queuedef _get(self):return self.queue.popleft()__class_getitem__ = classmethod(types.GenericAlias)

其重要体现队列的代码很少,大多数代码都在保证线程安全,如下:

    def _init(self, maxsize):self.queue = deque()def _qsize(self):return len(self.queue)# Put a new item in the queuedef _put(self, item):self.queue.append(item)# Get an item from the queuedef _get(self):return self.queue.popleft()

从上面可以看出,Queue的核心操作get、put都是基于deque实现的
其Queue类的定义如下:

class Queue(Generic[_T]):maxsize: intmutex: Lock  # undocumentednot_empty: Condition  # undocumentednot_full: Condition  # undocumentedall_tasks_done: Condition  # undocumentedunfinished_tasks: int  # undocumented# Despite the fact that `queue` has `deque` type,# we treat it as `Any` to allow different implementations in subtypes.queue: Any  # undocumenteddef __init__(self, maxsize: int = 0) -> None: ...def _init(self, maxsize: int) -> None: ...def empty(self) -> bool: ...def full(self) -> bool: ...def get(self, block: bool = True, timeout: float | None = None) -> _T: ...def get_nowait(self) -> _T: ...def _get(self) -> _T: ...def put(self, item: _T, block: bool = True, timeout: float | None = None) -> None: ...def put_nowait(self, item: _T) -> None: ...def _put(self, item: _T) -> None: ...def join(self) -> None: ...def qsize(self) -> int: ...def _qsize(self) -> int: ...def task_done(self) -> None: ...if sys.version_info >= (3, 9):def __class_getitem__(cls, item: Any) -> GenericAlias: ...

这些函数和属性是Python标准库中的queue.Queue类的接口。queue.Queue提供了一个线程安全的FIFO(First In, First Out)队列,常用于多线程环境下的任务管理。以下是每个函数和属性的详细介绍及其用法:

属性

  • maxsize: int
    队列的最大大小。如果 maxsize 为 0 或负数,则队列大小没有限制。

  • mutex: Lock
    一个锁对象,用于同步队列的访问,确保线程安全。

  • not_empty: Condition
    一个条件变量,用于在队列非空时进行通知。

  • not_full: Condition
    一个条件变量,用于在队列未满时进行通知。

  • all_tasks_done: Condition
    一个条件变量,用于在所有任务完成时进行通知。

  • unfinished_tasks: int
    未完成任务的计数。

  • queue: Any
    实际存储队列元素的容器,类型可以是任意的。该属性未公开文档。在_init中将其初始化。

方法

__init__(self, maxsize: int = 0) -> None

构造函数,用于初始化一个队列对象。

参数

  • maxsize: 队列的最大大小。如果为 0 或负数,则队列大小没有限制。

示例

from queue import Queueq = Queue(maxsize=10)
_init(self, maxsize: int) -> None

初始化队列的内部方法。通常由构造函数调用,不直接使用。

参数

  • maxsize: 队列的最大大小。
empty(self) -> bool

如果队列为空,返回 True

示例

>>> from queue import Queue
>>> q = Queue()
>>> q.empty()
True
full(self) -> bool

如果队列已满,返回 True

示例

>>> q.put(1)
>>> q.put(2)
>>> q.full()
True
>>> q.put(3) # 满了之后添加元素会一直等待

在这里插入图片描述

get(self, block: bool = True, timeout: float | None = None) -> _T

从队列中移除并返回一个元素。

参数

  • block: 如果为 True,在队列为空时会阻塞,直到有元素可用。
  • timeout: 阻塞的最大时间(以秒为单位)。如果为 None,将一直阻塞直到有元素可用。

示例

>>> from queue import Queue
>>> q = Queue()
>>> q.put(1)
>>> q.put(2)
>>> q 
<queue.Queue object at 0x0000020DD481AD30>
>>> list(q) # Queue是不可迭代类型
Traceback (most recent call last):File "<stdin>", line 1, in <module>
TypeError: 'Queue' object is not iterable
>>> print(q)
<queue.Queue object at 0x0000020DD481AD30>
>>> q.get() # FIFO
1
>>> q.get() # LILO
2
get_nowait(self) -> _T

非阻塞地从队列中移除并返回一个元素。如果队列为空,抛出 queue.Empty 异常。

示例

>>> q = Queue()
>>> q.put(1)
>>> q.get_nowait()
1
>>> q.get_nowait() # 不等待会报错 如果使用q.get()会一直等待,直到右元素入队
Traceback (most recent call last):File "<stdin>", line 1, in <module>File "D:\Python\Python36\lib\queue.py", line 192, in get_nowaitreturn self.get(block=False)File "D:\Python\Python36\lib\queue.py", line 161, in getraise Empty
queue.Empty
_get(self) -> _T

内部方法,用于从队列中获取一个元素。通常由 get 方法调用,不直接使用。

put(self, item: _T, block: bool = True, timeout: float | None = None) -> None

将一个元素放入队列。

参数

  • item: 要放入队列的元素。
  • block: 如果为 True,在队列已满时会阻塞,直到有空间可用。
  • timeout: 阻塞的最大时间(以秒为单位)。如果为 None,将一直阻塞直到有空间可用。

示例

>>> q = Queue(2)
>>> q.put(1)
>>> q.put(2)
>>> q.put(3,True,3) # 3秒之后报错
Traceback (most recent call last):File "<stdin>", line 1, in <module>File "D:\Python\Python36\lib\queue.py", line 141, in putraise Full
queue.Full
put_nowait(self, item: _T) -> None

非阻塞地将一个元素放入队列。如果队列已满,抛出 queue.Full 异常。

示例

>>> q = Queue(2)
>>> q.put(1)
>>> q.put(2)
>>> q.put_nowait(3) # 立即报错
Traceback (most recent call last):File "<stdin>", line 1, in <module>File "D:\Python\Python36\lib\queue.py", line 184, in put_nowaitreturn self.put(item, block=False)File "D:\Python\Python36\lib\queue.py", line 130, in putraise Full
queue.Full
_put(self, item: _T) -> None

内部方法,用于将一个元素放入队列。通常由 put 方法调用,不直接使用。

join(self) -> None

阻塞主线程,直到所有的队列项被处理。每次从 get 中获取一个项目后,需要调用 task_done 来表明该项目已经完成处理。

示例

import threading
import time
from queue import Queuedef worker(q):while True:item = q.get()if item is None:breakprint(f'Processing {item}')time.sleep(1)q.task_done()
q = Queue()
thread = threading.Thread(target=worker, args=(q,))
thread.start()
for item in range(5):q.put(item)
q.join()  # 阻塞,直到所有任务完成
q.put(None)  # 停止工作线程
thread.join()
qsize(self) -> int

返回队列中的元素数量。

示例

>>> q = Queue()
>>> q.put(1)
>>> q.put(2)
>>> q.qsize()
2
>>> len(q) #并没有实现__len__
Traceback (most recent call last):File "<stdin>", line 1, in <module>
TypeError: object of type 'Queue' has no len()
_qsize(self) -> int

内部方法,用于返回队列中的元素数量。通常由 qsize 方法调用,不直接使用。

task_done(self) -> None

表明一个队列中的任务已经完成。在每次从 get 中获取一个项目后调用。

示例

>>> q = Queue()
>>> q.put(1)
>>> q.get()
1
>>> q.task_done()

线程安全优先级队列

优先级队列继承了Queue对象,其主要是继承了它的线程安全性,其底层存储于deque没有任何关系了,就是数组,插入和弹出都换成了的上虑和下虑操作,如果你不知道堆是什么,请看我的另一篇博文图解堆队列算法,这里就不介绍了

class PriorityQueue(Queue):'''Variant of Queue that retrieves open entries in priority order (lowest first).Entries are typically tuples of the form:  (priority number, data).'''def _init(self, maxsize):self.queue = []def _qsize(self):return len(self.queue)def _put(self, item):heappush(self.queue, item)def _get(self):return heappop(self.queue)

那么PriorityQueue的调用方法是继承了Queue,方法使用时一模一样的。

线程安全——栈

LifoQueue的线程安全也是套壳Queue的,只是将初始化换成数组,getput换成我们使用的普通的栈的形式,就是直接拿数组作为栈append为入栈,pop为出栈。

class LifoQueue(Queue):'''Variant of Queue that retrieves most recently added entries first.'''def _init(self, maxsize):self.queue = []def _qsize(self):return len(self.queue)def _put(self, item):self.queue.append(item)def _get(self):return self.queue.pop()

那么LifoQueue的调用方法是继承了Queue,方法使用时一模一样的。

线程安全的简单的队列——SimpleQueue

SimpleQueue Queue的简化版,他没有了大小限制,但是也是线程安全的,剩余的putgetput_nowaitget_nowaitemptyqsize操作都相同。

class _PySimpleQueue:'''Simple, unbounded FIFO queue.This pure Python implementation is not reentrant.'''# Note: while this pure Python version provides fairness# (by using a threading.Semaphore which is itself fair, being based#  on threading.Condition), fairness is not part of the API contract.# This allows the C version to use a different implementation.def __init__(self):self._queue = deque()self._count = threading.Semaphore(0)def put(self, item, block=True, timeout=None):'''Put the item on the queue.The optional 'block' and 'timeout' arguments are ignored, as this methodnever blocks.  They are provided for compatibility with the Queue class.'''self._queue.append(item)self._count.release()def get(self, block=True, timeout=None):'''Remove and return an item from the queue.If optional args 'block' is true and 'timeout' is None (the default),block if necessary until an item is available. If 'timeout' isa non-negative number, it blocks at most 'timeout' seconds and raisesthe Empty exception if no item was available within that time.Otherwise ('block' is false), return an item if one is immediatelyavailable, else raise the Empty exception ('timeout' is ignoredin that case).'''if timeout is not None and timeout < 0:raise ValueError("'timeout' must be a non-negative number")if not self._count.acquire(block, timeout):raise Emptyreturn self._queue.popleft()def put_nowait(self, item):'''Put an item into the queue without blocking.This is exactly equivalent to `put(item, block=False)` and is only providedfor compatibility with the Queue class.'''return self.put(item, block=False)def get_nowait(self):'''Remove and return an item from the queue without blocking.Only get an item if one is immediately available. Otherwiseraise the Empty exception.'''return self.get(block=False)def empty(self):'''Return True if the queue is empty, False otherwise (not reliable!).'''return len(self._queue) == 0def qsize(self):'''Return the approximate size of the queue (not reliable!).'''return len(self._queue)__class_getitem__ = classmethod(types.GenericAlias)if SimpleQueue is None:SimpleQueue = _PySimpleQueue

相关文章:

python基础-数据结构-leetcode刷题必看-queue---队列-python的底层构建

文章目录 队列双端队列 deque底层存储deque接口1. __init__(self, iterable: Iterable[_T], maxlen: int | None None) -> None2. append(self, __x: _T) -> None3. appendleft(self, __x: _T) -> None4. copy(self) -> Self5. count(self, __x: _T) -> int6. …...

深入理解Spring Security:保护你的Web应用程序

深入理解Spring Security:保护你的Web应用程序 这听起来像是一部詹姆斯邦德电影,邦德试图进入坏家伙的藏身之处。坏家伙设置了一系列超级安全措施,有多层次的安全防御。邦德克服了其中一层,进入了隐藏处,但又遇到了下一个陷阱。他战胜了一个又一个陷阱,最终克服了所有障…...

【车载开发系列】Vector工具链的安装

【车载开发系列】Vector工具链的安装 【车载开发系列】Vector工具链的安装 【车载开发系列】Vector工具链的安装一. VectorDriver二. DaVinci_Developer三. DaVinci Configurator 一. VectorDriver Vector Driver Setup是Vector产品链中重要的驱动软件,所有的硬件设备进行连接…...

Windows系统部署YOLOv5 v6.1版本的训练与推理环境保姆级教程

文章目录 一 概述二 依赖环境(prerequisites)2.1 硬件环境2.2 软件环境 三 环境安装3.1 创建并激活虚拟环境3.2 安装Pytorch与torchvision3.3 校验Pytorch安装3.4 下载 YOLOv5 v6.1 源码3.5 安装 YOLOv5 依赖3.6 下载预训练模型3.7 安装其他依赖3.8 测试环境安装3.9 测试训练流…...

[RK3588-Android12] 关于EDP屏外设为Panel,不支持HPD的配置

问题描述 直接附上dts配置&#xff0c;也可自行查看先关文档RKDocs\common\display\Rockchip_RK3588_User_Guide_eDP_CN.pdf 解决方案&#xff1a; // EDP屏参数panel-edp {compatible "simple-panel";// 屏en脚 自行根据原理图配置enable-gpios <&gpioX R…...

142.栈和队列:用栈实现队列(力扣)

题目描述 代码解决 class MyQueue { public:stack<int> stIn; // 输入栈&#xff0c;用于push操作stack<int> stOut; // 输出栈&#xff0c;用于pop和peek操作MyQueue() {}void push(int x) {stIn.push(x); // 将元素压入输入栈}int pop() {// 如果输出栈为空&…...

乡村振兴的乡村产业创新发展:培育乡村新兴产业,打造乡村产业新名片,促进乡村经济多元化发展

目录 一、引言 二、乡村产业创新发展的必要性 &#xff08;一&#xff09;适应新时代发展要求 &#xff08;二&#xff09;满足消费升级需求 &#xff08;三&#xff09;促进农民增收致富 三、培育乡村新兴产业策略 &#xff08;一&#xff09;加强科技创新引领 &#…...

数据库|基于T-SQL创建数据库

哈喽&#xff0c;你好啊&#xff0c;我是雷工&#xff01; SQL Server用于操作数据库的编程语言为Transaction-SQL,简称T-SQL。 本节学习基于T-SQL创建数据库。以下为学习笔记。 01 打开新建查询 首先连接上数据库&#xff0c;点击【新建查询】打开新建查询窗口&#xff0c; …...

智能家居ZigBee网关选型定制指南:主控,操作系统,天线设计,助力IoT开发者

随着科技的发展和人们生活水平的提高&#xff0c;智能家居以其便捷、舒适、安全等特点&#xff0c;逐渐走进千家万户&#xff0c;成为家装消费品换新升级的重要方向。在智能家居系统中&#xff0c;网关扮演着中枢控制器的角色&#xff0c;负责将各种设备连接到互联网上&#xf…...

QT截图程序,可多屏幕截图二,增加调整截图区域功能

上一篇QT截图程序&#xff0c;可多屏幕截图只是实现了最基本的截图功能&#xff0c;虽然能用但是缺点也有&#xff0c;没办法更改选中的区域&#xff0c;这在实际使用时不太方便。这篇增加了这个功能。先看看效果。 实现代码为&#xff1a; 头文件 #ifndef MASKWIDGET_H #de…...

开源浪潮与闭源堡垒:大模型未来的双重奏

从数据隐私、商业应用和社区参与等方面来看&#xff0c;开源大模型和闭源大模型各有优劣势。开源模型在透明度、社区协作和成本效益方面具有优势&#xff0c;而闭源模型在安全性、合规性和商业竞争力方面表现出色。因此&#xff0c;我更倾向于认为&#xff0c;未来的大模型发展…...

postman教程-6-发送delete请求

领取资料&#xff0c;咨询答疑&#xff0c;请➕wei: June__Go 上一小节我们学习了postman发送put请求的方法&#xff0c;本小节我们讲解一下postman发送delete请求的方法。 HTTP DELETE 请求是一种用于删除指定资源的请求方法。在RESTful API 设计中&#xff0c;DELETE 请求…...

java小技能: 数字和字母组合的验证码图片(生成验证码字符并加上噪点,干扰线)

文章目录 引言I 验证码的作用1.1 验证使用计算机的是一个人,而非计算机程序。1.2 提供一个很短的时间窗的一次性密码。II 数字和字母组合的验证码图片2.1 获取验证码图片2.2 生成验证码字符并加上噪点,干扰线see also引言 世界上没有绝对的信息安全,但是有防范得好和坏的分…...

网络故障与排除

一、Router-ID冲突导致OSPF路由环路 路由器收到相同Router-ID的两台设备发送的LSA&#xff0c;所以查看路由表看到的OSPF缺省路由信息就会不断变动。而当C1的缺省路由从C2中学到&#xff0c;C2的缺省路由又从C1中学到时&#xff0c;就形成了路由环路&#xff0c;因此出现路由不…...

Cocos Creator 编辑器的数据绑定详解

Cocos Creator 是一款由 Cocos 平台推出的游戏开发工具&#xff0c;它集成了图形化编辑器、脚本引擎和资源管理器等功能&#xff0c;方便开发者快速地创建游戏。其中&#xff0c;数据绑定是 Cocos Creator 编辑器中非常重要的一个功能&#xff0c;它可以帮助开发者实现页面元素…...

解决Selenium NameError: name ‘By’ is not defined

解决Selenium NameError: name ‘By’ is not defined 文章目录 解决Selenium NameError: name By is not defined背景错误原因解决方法1. 检查导入语句2. 修正拼写和大小写3. 验证Selenium库安装4. 重启IDE或终端5. 检查环境变量 验证总结 背景 在使用Selenium进行Web自动化测…...

创建特定结构的二维数组:技巧与示例

新书上架~&#x1f447;全国包邮奥~ python实用小工具开发教程http://pythontoolsteach.com/3 欢迎关注我&#x1f446;&#xff0c;收藏下次不迷路┗|&#xff40;O′|┛ 嗷~~ 目录 一、引言&#xff1a;二维数组的奇妙世界 二、方法一&#xff1a;直接初始化 1. 初始化一个…...

React Native 之 BackHandler (二十)

react-native 中的 BackHandler 是一个用于处理 Android 设备上的硬件返回按钮&#xff08;back button&#xff09;和 iOS 设备上的手势返回&#xff08;swipe back gesture&#xff09;的模块。在 React Native 应用中&#xff0c;当用户按下返回按钮或执行返回手势时&#x…...

一篇文章讲透排序算法之快速排序

前言 本篇博客难度较高&#xff0c;建议在学习过程中先阅读一遍思路、浏览一遍动图&#xff0c;之后研究代码&#xff0c;之后仔细体会思路、体会动图。之后再自己进行实现。 一.快排介绍与思想 快速排序相当于一个对冒泡排序的优化&#xff0c;其大体思路是先在文中选取一个…...

kubernetes-PV与PVC、存储卷

一、PV和PVC详解 当前&#xff0c;存储的方式和种类有很多&#xff0c;并且各种存储的参数也需要非常专业的技术人员才能够了解。在Kubernetes集群中&#xff0c;放了方便我们的使用和管理&#xff0c;Kubernetes提出了PV和PVC的概念&#xff0c;这样Kubernetes集群的管理人员就…...

643. 子数组最大平均数 I

给你一个由 n 个元素组成的整数数组 nums 和一个整数 k 。 请你找出平均数最大且 长度为 k 的连续子数组&#xff0c;并输出该最大平均数。 任何误差小于 10-5 的答案都将被视为正确答案。 示例 1&#xff1a; 输入&#xff1a;nums [1,12,-5,-6,50,3], k 4 输出&#xff…...

Node性能如何进行监控以及优化?

一、 是什么 Node作为一门服务端语言&#xff0c;性能方面尤为重要&#xff0c;其衡量指标一般有如下&#xff1a; CPU内存I/O网络 CPU 主要分成了两部分&#xff1a; CPU负载&#xff1a;在某个时间段内&#xff0c;占用以及等待CPU的进程总数CPU使用率&#xff1a;CPU时…...

ToList()和ToArray()的区别

以下是具体分析&#xff1a; 1. 返回类型 ToList()&#xff1a;返回一个泛型列表 List<T>&#xff0c;其中 T 是列表中元素的类型。 ToArray()&#xff1a;返回一个 Object 类型的数组。如果需要特定类型的数组&#xff0c;必须使用重载的 ToArray(T[] a) 方法&#x…...

11.RedHat认证-Linux文件系统(中)

11.RedHat认证-Linux文件系统(中) Linux的文件系统 格式化分区(1道题) #对于Linux分区来说&#xff0c;只有格式化之后才能使用&#xff0c;不格式化是无法使用的。 #Linux分区格式化之后就会变成文件系统&#xff0c;格式化的过程相当于对分区做了一个文件系统。 #Linux常见…...

windows系统电脑外插键盘驱动出现感叹号或者显示未知设备,键盘无法输入的解决办法

笔记本外插的键盘不能用&#xff0c;鼠标可以使用。 查找故障&#xff0c;结果打开设备管理器看到键盘那项里是一个的黄色惊叹号显示未知设备&#xff01;[图片]如下图所示 其实解决办法很简单&#xff0c;不要相信网上的一些博主说删除什么注册表&#xff0c;我开始跟着他们操…...

【开源项目】Excel数据表自动生成工具v1.0版

一、介绍 Excel数据表自动生成工具是Go语言编写的一款小型工具软件&#xff0c;用于将特定的Excel表格内容导出为多种编程语言的代码或可以直接读取的数据内容。 开源Github地址&#xff1a;https://github.com/SkyCreator/goproj 二、版本v1.0功能概览 1.编程语言支持 目前…...

Docker-一文详解容器通信的基础网络模式及衍生的自定义网络模式

启动容器时&#xff0c;通过-p 宿主机端口:容器端口&#xff0c;就可以通过访问宿主机端口访问到容器&#xff0c;这种原理机制是啥&#xff0c;有没有其它方式可以让宿主机和容器通信&#xff0c;以及容器与容器之间如何通信。带着这几个问题开始学习Docker的网络知识。 文章…...

Convolutional Occupancy Networks【ECCV】

论文&#xff1a;https://arxiv.org/pdf/2003.04618 代码&#xff1a;GitHub - autonomousvision/convolutional_occupancy_networks: [ECCV20] Convolutional Occupancy Networks 图 1&#xff1a;卷积占据网络。传统的隐式模型 (a) 由于其全连接网络结构&#xff0c;表现能力…...

Android Studio 问题集锦

报 Plugin [id: ‘com.android.application’, version: ‘8.1.3’, apply: false] was not found in any of the following sources: 场景&#xff1a;在一个Android 11的项目中添加一个Android 9的项目作为其Module&#xff0c;结果导致原项目无法正常运行&#xff0c;且原项…...

J.搬砖【蓝桥杯】/01背包+贪心

搬砖 01背包贪心 思路&#xff1a;要让重量更小的在更前面&#xff0c;价值更大的在更后面&#xff0c;vi−wj>vj−wi viwi>vjwj 第 i 个箱子放在第 j 个箱子下面就显然更优。所以进行排序再用01背包即可。 #include<iostream> #include<algorithm> #defi…...