More precise sleep (usleep) in Python?

284 Views Asked by At

I'm trying to implement real-time plotting in Python, with samples being around 500-1000 microseconds apart. Using time.sleep() between drawing each sample doesn't work due to reasons mentioned here: accuracy of sleep(). I'm currently doing busy waiting like this:

stime = time()
while stime + diff/1000000 > time():
    pass

But it's taking a lot of CPU resources and it's also not 100% precise. Is there a better way of doing this (preferably platform independent and not busy waiting)?

1

There are 1 best solutions below

0
bcattle On

Why not just call usleep directly:

import ctypes
from ctypes.util import find_library

libc = find_library('c')

libc.usleep(1)

This should work on Linux or Mac.