Quantcast
Channel: How to make a timestamp atomic/interrupt safe in Python? - Stack Overflow
Viewing all articles
Browse latest Browse all 2

How to make a timestamp atomic/interrupt safe in Python?

$
0
0

This should be easy. After looking at what's going on, I'm not so sure. Is the writing/reading of a single binary integer atomic? That's how the underlying hardware does reads and writes of 32-bit integers. After some research, I realized Python does not store integers as a collection of bytes. It doesn't even store bytes as a collection of bytes. There's overhead involved. Does this overhead break the atomic nature of binary integers?

Here is some code I used trying to figure this out:

import timeimport systm=time.time()int_tm = int(tm * 1000000)bin_tm = bin(int_tm)int_bin_tm = int(bin_tm, 2)print('tm:', tm, ", Size:", sys.getsizeof(tm))print('int_tm:', int_tm, ", Size:", sys.getsizeof(int_tm))print('bin_tm:', bin_tm, ", Size:", sys.getsizeof(bin_tm))print('int_bin_tm:', int_bin_tm, ", Size:", sys.getsizeof(int_bin_tm))

Output:

tm: 1581435513.076924 , Size: 24int_tm: 1581435513076924 , Size: 32bin_tm: 0b101100111100100111010100101111111011111110010111100 , Size: 102int_bin_tm: 1581435513076924 , Size: 32

For a couple side questions, does Python's binary representation of integers really consume so much more memory? Am I using the wrong type for converting decimal integers to bytes?


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images