Python 2.x - sleep call in milliseconds on Windows

I was given very good tips on this forum on how to encode the clock object in Python 2. I now have code. This is a clock that ticks at 60 FPS:

import sys
import time

class Clock(object):

    def __init__(self):

        self.init_os()
        self.fps = 60.0
        self._tick = 1.0 / self.fps
        print "TICK", self._tick
        self.check_min_sleep()
        self.t = self.timestamp()

    def init_os(self):

        if sys.platform == "win32":
            self.timestamp = time.clock
            self.wait = time.sleep

    def timeit(self, f, args):

        t1 = self.timestamp()
        f(*args)
        t2 = self.timestamp()
        return t2 - t1

    def check_min_sleep(self):
        """checks the min sleep time on the system"""

        runs =  1000
        times = [self.timeit(self.wait, (0.001, )) for n in xrange(runs)]
        average = sum(times) / runs
        print "average min sleep time:", round(average, 6)
        sort = sorted(times)
        print "fastest, slowest", sort[0], sort[-1]

    def tick(self):

        next_tick = self.t + self._tick
        t = self.timestamp()
        while t < next_tick:
            t = self.timestamp()
        self.t = t

if __name__ == "__main__":
    clock = Clock()

The clock is not too bad, but in order to avoid a busy cycle, I would like Windows to sleep less than usually about 15 milliseconds. On my system (64-bit Windows 10), it returns to me an average of about 15/16 ms when the clock starts, if Python is the only application running. This path is too long for minimal sleep to avoid a busy cycle.

Does anyone know how I can make Windows sleep less than this value?

0
1

wPeriodMin, timeGetDevCaps. timer_resolution, timeBeginPeriod timeEndPeriod.

import timeit
import contextlib
import ctypes
from ctypes import wintypes

winmm = ctypes.WinDLL('winmm')

class TIMECAPS(ctypes.Structure):
    _fields_ = (('wPeriodMin', wintypes.UINT),
                ('wPeriodMax', wintypes.UINT))

def _check_time_err(err, func, args):
    if err:
        raise WindowsError('%s error %d' % (func.__name__, err))
    return args

winmm.timeGetDevCaps.errcheck = _check_time_err
winmm.timeBeginPeriod.errcheck = _check_time_err
winmm.timeEndPeriod.errcheck = _check_time_err

@contextlib.contextmanager
def timer_resolution(msecs=0):
    caps = TIMECAPS()
    winmm.timeGetDevCaps(ctypes.byref(caps), ctypes.sizeof(caps))
    msecs = min(max(msecs, caps.wPeriodMin), caps.wPeriodMax)
    winmm.timeBeginPeriod(msecs)
    yield
    winmm.timeEndPeriod(msecs)

def min_sleep():
    setup = 'import time'
    stmt = 'time.sleep(0.001)'
    return timeit.timeit(stmt, setup, number=1000)

>>> min_sleep()
15.6137827

>>> with timer_resolution(msecs=1): min_sleep()
...
1.2827173000000016

with:

>>> min_sleep()
15.6229814
+2

Source: https://habr.com/ru/post/1648394/


All Articles