1

I would like to try some hardware testing with python. I have to send commands to the hardware where there is a specification for consecutive bytes transmission time interval (~100-200us). However, I found the sleep() method in time module unstable when the delay time is too small. Is there any way/operations that takes ~100-200us?

I also wonder what is the accuracy of the function time(), can it really time a 100us interval?

from time import time, sleep
a = []
start = time()
for i in range(10):
    sleep(200e-6)
    a.append(time()-start)

this is what 'a' looks like:

[0.0010325908660888672,
 0.004021644592285156,
 0.006222248077392578,
 0.008239507675170898,
 0.009252071380615234,
 0.01157999038696289,
 0.013728857040405273,
 0.014998674392700195,
 0.016725540161132812,
 0.0187227725982666]
CypherX
  • 6,127
  • 3
  • 16
  • 30
Mayan
  • 432
  • 3
  • 10
  • 2
    I guess your measurement has a large impact on the result already. Use the `timeit` module to get a more acurate measurement. – Klaus D. Oct 25 '19 at 06:19
  • 1
    A desktop operating system has lots of things going on as well as Python executing your code - the mouse moves, The clock ticks over a second/minute so the displayed clock is automatically updated,other programs do stuff, there is i/o on the network, etc. so Yu will always is different from a realtime operating system so while you can specify the value to `time.sleep()` as precisely as you want, that is the _minimum_ delay you will get, not the maximum. – DisappointedByUnaccountableMod Oct 25 '19 at 06:24
  • Have you read the documentation for `time.sleep()`? https://docs.python.org/3/library/time.html – DisappointedByUnaccountableMod Oct 25 '19 at 06:27
  • @KlausD. I tried your suggestion. Seems that the lower limit is 1.7ms for the sleep() function.@barny I tried to mannually set the priority of the python programme to high in the task manager. The time between consecutive sleep is more stable, though the lower limit is ~1.7ms – Mayan Oct 25 '19 at 06:33
  • Linux or Windows is not a real-time or predictable environment timing-wise when you get to sub-10ms-ish intervals. Try doing some heavy downloading see how your timing stability varies? – DisappointedByUnaccountableMod Oct 25 '19 at 15:21

2 Answers2

2

About accuracy of time.sleep

It looks like time.sleep() only has millisecond resolution. Though it could range between a few milliseconds to over 20 milliseconds (best case scenarios), based on what OS you are using, it does not look like you will get accurate microsecond sleep execution from it.

References

  1. Must See This: How accurate is python's time.sleep()?
  2. usleep in Python
  3. https://github.com/JuliaLang/julia/issues/12770
  4. https://gist.github.com/ufechner7/1aa3e96a8a5972864cec

Another Option:

You could potentially then look for creating a C/C++ wrapper around the timer-code to use it from Python.

CypherX
  • 6,127
  • 3
  • 16
  • 30
1

The time module is not unstable, it is the underlying OS that you are running on. The request of sleeping is just passed on to the OS. In the documentation, one can read that the actual sleep time may be less then requested as any caught signal suspends the sleep, or it can be longer than requested as of scheduling reasons.If you need very high accuracy, you can use an Real-Time OS.

Daniel
  • 81
  • 3