Accurate timestamping in Python logging -
i've been building error logging app , after way of accurately timestamping incoming data. when accurately mean each timestamp should accurate relative each other (no need sync atomic clock or that).
i've been using datetime.now() first stab, isn't perfect:
>>> 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 first second of samples looks 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
so looks timer data updated every ~15-32ms on machine. problem comes when come analyse data because sorting other timestamp , sorting timestamp again can leave data in wrong order (chronologically). nice have time stamps accurate point call time stamp generator gives unique timestamp.
i had been considering methods involving using time.clock() call added starting datetime, appreciate solution work accurately across threads on same machine. suggestions gratefully received.
you're unlikely sufficiently fine-grained control can eliminate possibility of duplicate timestamps - you'd need resolution smaller time takes generate datetime object. there couple of other approaches might take deal it:
deal it. leave timestamps non-unique are, rely on python's sort being stable deal reordering problems. sorting on timestamp first, else retain timestamp ordering - have careful start timestamp ordered list every time, rather doing multiple sorts on same list.
append own value enforce uniqueness. eg. include incrementing integer value part of key, or append such value if timestamps different. eg.
the following guarantee unique timestamp values:
class timestamper(object): def __init__(self): self.lock = threading.lock() self.prev = none self.count = 0 def gettimestamp(self): 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 threads), gets bit trickier though.
Comments
Post a Comment