How can a service retain just its last three inference messages?

A bounded deque evicts from the opposite end when an append would exceed its maximum length.

Python example and interpretation

Appending four messages from the right keeps messages two through four. This window suits diagnostics; a complete audit needs separate history.

from collections import deque
q=deque(maxlen=3)
q.extend(['r1','r2','r3','r4'])
list(q)

Result ['r2', 'r3', 'r4']

中文

如何只保留最近三条推理日志?

有 maxlen 的 deque 在一端追加且已满时,会从另一端移除最早占位的元素。

Python 算例与适用条件

四条日志连续从右端写入,最后只保留第 2 至第 4 条。滑窗适合现场诊断,完整审计需要另存历史。

from collections import deque
q=deque(maxlen=3)
q.extend(['r1','r2','r3','r4'])
list(q)

结果 ['r2', 'r3', 'r4']

References / 参考来源

Keep reading / 继续阅读