我正在监视特定事件的json流。我想计算在特定时间内发生的事件数量,比如每分钟事件数,然后与高水位线进行比较以采取行动。
for l in s.makefile() :
m = json.loads(l)
if 'Event' in m and m['Event'] == 'StarLost' :
print 'Star Lost at time= ' + str(datetime.datetime.fromtimestamp(m['Timestamp']))
答案 0 :(得分:1)
保留double-ended queue并根据您从json流获取的时间戳值插入和弹出项目。
import datetime
from collections import deque
threshold = 4
queue = deque()
for l in s.makefile() :
# fill the queue
record = json.loads(l)
try:
if record['Event'] == 'StarLost':
timestamp = datetime.datetime.fromtimestamp(record['Timestamp'])
print('Star Lost at time {}.'.format(timestamp.isoformat()))
queue.append((timestamp, record))
except KeyError:
pass # there was no 'Event' in the record
# clear the queue from old records
try:
while queue[0][0] < datetime.datetime.now() - datetime.timedelta(seconds=60):
queue.popleft()
except IndexError:
pass # there are no records in the queue.
# analyze the queue
if len(queue) > threshold:
print('There were more than {} events over the last minute!'.format(threshold))
此解决方案假设您s.makefile()
不停地生成json数据。
理想情况下,您将清除队列的部分和分析队列的部分放在填充队列的线程的不同线程中,但是如果您不能自己提出解决方案,那么{ {3}}现在不适合你,虽然我通过使用线程安全的deque
开始了它。如果你想更好地解决这个问题,请阅读它。