gpt4 book ai didi

python - 在 Python 中丢弃具有最大容量的 FIFO 队列?

转载 作者:太空宇宙 更新时间:2023-11-04 07:41:59 26 4
gpt4 key购买 nike

我需要一个丢弃 FIFO 队列,当它变满时自动丢弃项目。它不必是线程安全的。效率如果更重要的话。

也就是说,我需要对来自设备的信号进行采样,同时能够在半随机时间检索最后 n 秒(对象)。

我自己实现了一个(不是那么线程安全的)缓冲区,但感觉我正在重新发明轮子。此外,我们每秒谈论 100 个对象。大多数将被丢弃,例如必须一次检索 3000(= 30 秒的数据)(比如每十分钟一次)。

Python 标准库或其他地方是否已经存在这样的类?我使用了一些 goggle-fu,但未能找到任何有用的东西。

丢弃缓冲区

from Queue import Queue, Full, Empty
import logging


class DiscardingBuffer():
def __init__(self, capacity=0):
self._queue = Queue(maxsize=capacity)

def put(self, item):
while True:
try:
self._queue.put(item, block=False)
logging.debug('Put item: {0}'.format(item))
break
except Full:
discarded_item = self._queue.get(block=False)
logging.debug('Buffer is full. Discarding: {0}'.format(discarded_item))

def flush(self):
items = []

while True:
try:
items.append(self._queue.get(block=False))
except Empty:
logging.debug('Buffer is now empty.')
break

return items


def main():
buf = DiscardingBuffer(5)

for i in xrange(10):
buf.put(i)

logging.debug('Remaining items: {0}'.format(buf.flush()))
logging.debug('Verify it is empty: {0}'.format(buf.flush()))


if __name__ == '__main__':
logging.basicConfig(level=logging.DEBUG,
format='[%(levelname)1.1s %(asctime)s %(name)s (%(process)d):%(lineno)d] %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
main()

输出

[D 2013-08-22 10:13:58 root (4164):13] Put item: 0
[D 2013-08-22 10:13:58 root (4164):13] Put item: 1
[D 2013-08-22 10:13:58 root (4164):13] Put item: 2
[D 2013-08-22 10:13:58 root (4164):13] Put item: 3
[D 2013-08-22 10:13:58 root (4164):13] Put item: 4
[D 2013-08-22 10:13:58 root (4164):17] Buffer is full. Discarding: 0
[D 2013-08-22 10:13:58 root (4164):13] Put item: 5
[D 2013-08-22 10:13:58 root (4164):17] Buffer is full. Discarding: 1
[D 2013-08-22 10:13:58 root (4164):13] Put item: 6
[D 2013-08-22 10:13:58 root (4164):17] Buffer is full. Discarding: 2
[D 2013-08-22 10:13:58 root (4164):13] Put item: 7
[D 2013-08-22 10:13:58 root (4164):17] Buffer is full. Discarding: 3
[D 2013-08-22 10:13:58 root (4164):13] Put item: 8
[D 2013-08-22 10:13:58 root (4164):17] Buffer is full. Discarding: 4
[D 2013-08-22 10:13:58 root (4164):13] Put item: 9
[D 2013-08-22 10:13:58 root (4164):26] Buffer is now empty.
[D 2013-08-22 10:13:58 root (4164):38] Remaining items: [5, 6, 7, 8, 9]
[D 2013-08-22 10:13:58 root (4164):26] Buffer is now empty.
[D 2013-08-22 10:13:58 root (4164):39] Verify it is empty: []

最佳答案

使用collections.deque指定一个 maxlen,

>>> q = deque(maxlen=2)
>>> q.extend([1, 2, 3])
>>> q
deque([2, 3], maxlen=2)

关于python - 在 Python 中丢弃具有最大容量的 FIFO 队列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18375188/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com