Skip to content

Instantly share code, notes, and snippets.

@elvinio
Created March 14, 2016 10:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elvinio/953cae294a6320b67d40 to your computer and use it in GitHub Desktop.
Save elvinio/953cae294a6320b67d40 to your computer and use it in GitHub Desktop.
Return clock_gettime in python by invoking librt system call
#!/usr/bin/python
import ctypes, os
class timespec(ctypes.Structure):
_fields_ = [
('tv_sec', ctypes.c_long),
('tv_nsec', ctypes.c_long)
]
librt = ctypes.CDLL('librt.so.1', use_errno=True)
clock_gettime = librt.clock_gettime
clock_gettime.argtypes = [ctypes.c_int, ctypes.POINTER(timespec)]
def realtime():
t = timespec()
if clock_gettime(0, ctypes.pointer(t)) != 0:
errno_ = ctypes.get_errno()
raise OSError(errno_, os.strerror(errno_))
print t.tv_nsec
#return str(t.tv_sec) + "." + str(t.tv_nsec)
if __name__ == "__main__":
for i in range(0, 100):
realtime()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment