Python日志记录中的准确时间戳 [英] Accurate timestamping in Python logging

查看:590
本文介绍了Python日志记录中的准确时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近一直在构建一个错误记录应用程序,并且正在使用一种精确地为输入数据加上时间戳的方法.当我说准确时,我的意思是每个时间戳相对于彼此都应该是准确的(不需要同步到原子钟或类似的时钟).

I've been building an error logging app recently and was after a way of accurately timestamping the incoming data. When I say accurately I mean each timestamp should be accurate relative to each other (no need to sync to an atomic clock or anything like that).

我一直使用datetime.now()作为第一个刺,但这并不完美:

I've been using datetime.now() as a first stab, but this isn't perfect:

>>> for i in range(0,1000):
...     datetime.datetime.now()
...
datetime.datetime(2008, 10, 1, 13, 17, 27, 562000)
datetime.datetime(2008, 10, 1, 13, 17, 27, 562000)
datetime.datetime(2008, 10, 1, 13, 17, 27, 562000)
datetime.datetime(2008, 10, 1, 13, 17, 27, 562000)
datetime.datetime(2008, 10, 1, 13, 17, 27, 578000)
datetime.datetime(2008, 10, 1, 13, 17, 27, 578000)
datetime.datetime(2008, 10, 1, 13, 17, 27, 578000)
datetime.datetime(2008, 10, 1, 13, 17, 27, 578000)
datetime.datetime(2008, 10, 1, 13, 17, 27, 578000)
datetime.datetime(2008, 10, 1, 13, 17, 27, 609000)
datetime.datetime(2008, 10, 1, 13, 17, 27, 609000)
datetime.datetime(2008, 10, 1, 13, 17, 27, 609000)
etc.

采样的第一秒在时钟之间的变化如下:

The changes between clocks for the first second of samples looks like this:

uSecs    difference
562000  
578000  16000
609000  31000
625000  16000
640000  15000
656000  16000
687000  31000
703000  16000
718000  15000
750000  32000
765000  15000
781000  16000
796000  15000
828000  32000
843000  15000
859000  16000
890000  31000
906000  16000
921000  15000
937000  16000
968000  31000
984000  16000

因此,计时器数据似乎仅在我的计算机上每隔15-32ms更新一次.当我们来分析数据时,问题就来了,因为按除时间戳以外的其他内容进行排序,然后再次按时间戳进行排序,可能会使数据以错误的顺序(按时间顺序)排列.最好使时间戳准确到任何对时间戳生成器的调用都提供唯一时间戳的地步.

So it looks like the timer data is only updated every ~15-32ms on my machine. The problem comes when we come to analyse the data because sorting by something other than the timestamp and then sorting by timestamp again can leave the data in the wrong order (chronologically). It would be nice to have the time stamps accurate to the point that any call to the time stamp generator gives a unique timestamp.

我一直在考虑一些方法,这些方法涉及使用添加到开始日期时间的time.clock()调用,但是希望能够在同一台计算机上的各个线程之间准确地工作的解决方案.任何建议将不胜感激.

I had been considering some methods involving using a time.clock() call added to a starting datetime, but would appreciate a solution that would work accurately across threads on the same machine. Any suggestions would be very gratefully received.

推荐答案

您不太可能获得足够细粒度的控制以完全消除可能性 重复的时间戳记-您需要的分辨率要小于生成datetime对象所需的时间.您可能还可以使用其他几种方法来处理它:

You're unlikely to get sufficiently fine-grained control that you can completely eliminate the possibility of duplicate timestamps - you'd need resolution smaller than the time it takes to generate a datetime object. There are a couple of other approaches you might take to deal with it:

  1. 处理.让您的时间戳保持不变,但是要依靠python的排序稳定来处理重新排序问题.按照时间戳 first 排序,然后其他东西将保留时间戳的顺序-您只需要注意始终每次都从时间戳排序列表开始,而不要对同一列表进行多种排序.

  1. Deal with it. Leave your timestamps non-unique as they are, but rely on python's sort being stable to deal with reordering problems. Sorting on timestamp first, then something else will retain the timestamp ordering - you just have to be careful to always start from the timestamp ordered list every time, rather than doing multiple sorts on the same list.

附加您自己的值以强制唯一性.例如.包括一个递增的整数值作为键的一部分,或者仅在时间戳不同时才附加此值.例如,

Append your own value to enforce uniqueness. Eg. include an incrementing integer value as part of the key, or append such a value only if timestamps are different. Eg.

以下内容将保证唯一的时间戳值:

The following will guarantee unique timestamp values:

    class TimeStamper(object):
        def __init__(self):
            self.lock = threading.Lock()
            self.prev = None
            self.count = 0

         def getTimestamp(self):
             with self.lock:
                 ts = str(datetime.now())
                 if ts == self.prev:
                     ts +='.%04d' % self.count
                     self.count += 1
                 else:
                     self.prev = ts
                     self.count = 1
             return ts

对于多个进程(而不是线程),它有点棘手.

For multiple processes (rather than threads), it gets a bit trickier though.

这篇关于Python日志记录中的准确时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆