Sleep for accurate time in python

I need to wait about 25 ms in one of my functions. Sometimes this function is called when the processor is busy with other things and in other cases it has the processor for itself.

I tried time.sleep(.25) , but sometimes its actually 25 ms and other times it takes a lot longer. Is there a way to sleep for a while regardless of processor availability?

+4
source share
6 answers

Since you are working with a preventive operating system , you cannot guarantee that your process will be able to control the CPU for 25 ms.

If you still want to try, it would be better to have a busy cycle that will poll up to 25 ms. Maybe something like this:

 import time target_time = time.clock() + 0.025 while time.clock() < target_time: pass 
+8
source

0.25 seconds - 250 ms, not 25. In addition, there is no way to wait exactly 25 ms in conventional operating systems - you will need a real-time operating system.

+6
source

What system do you work on? If you are on Windows, you can do something similar for accurate time:

 import ctypes kernel32 = ctypes.windll.kernel32 # This sets the priority of the process to realtime--the same priority as the mouse pointer. kernel32.SetThreadPriority(kernel32.GetCurrentThread(), 31) # This creates a timer. This only needs to be done once. timer = kernel32.CreateWaitableTimerA(ctypes.c_void_p(), True, ctypes.c_void_p()) # The kernel measures in 100 nanosecond intervals, so we must multiply .25 by 10000 delay = ctypes.c_longlong(.25 * 10000) kernel32.SetWaitableTimer(timer, ctypes.byref(delay), 0, ctypes.c_void_p(), ctypes.c_void_p(), False) kernel32.WaitForSingleObject(timer, 0xffffffff) 

This code will greatly guarantee that your process will sleep .25 seconds. However, pay attention: you may want to lower the priority to 2 or 3 if it is not absolutely critical that he sleeps for 0.25 seconds. Of course, do not change the priority too high for the final product.

+4
source

What you intend to do is a real-time application. Python (and probably the OS you are using) is not intended to program such applications where the time limit is so strict.

In order for you to achieve what you are looking for, you need a real-time OS (RTOS) and develop the application using a suitable programming language (usually C) after the best RT practices.

+2
source

From the docs of the sleep method:

Pause execution for a specified number of seconds. The argument may be a floating point number to indicate a more accurate sleep time. the actual pause time may be less than requested because any signal caught will stop sleep () after this signal has been executed. In addition, the suspension time may be longer than that requested by an arbitrary amount due to the planning of other activities in the system.

The fact is that it depends on your base OS.

+1
source

Edit: On Windows 10, this nonsense seems unnecessary. Try it like this:

 >>> from time import sleep >>> import timeit >>> '%.2f%% overhead' % (timeit.timeit('sleep(0.025)', number=100, globals=globals()) / 0.025 - 100) '0.29% overhead' 

.29% or so, rather low overhead and usually more than fairly accurate.

Previous versions of Windows will by default have a sleep resolution of 55 ms, which means that your sleep call will take from 25 to 55 ms. To get the sleep resolution to 1 millisecond, you need to set the resolution used by Windows by calling timeBeginPeriod :

 import ctypes winmm = ctypes.WinDLL('winmm') winmm.timeBeginPeriod(1) 
0
source

All Articles