0

i have a python project that should check a situation repeatedly in a certain time and it must check it under 3ms at least.But unfortunately my code checks it in between 14ms-16ms so it is not working.But it was working before i updated my windows to 2004 and now i can't fix the problem.I tried removing updates and all but didnt work.

Here is an example code:

import time
now=time.perf_counter()
lastpoint=3
c=1
while now<lastpoint:

    time.sleep(0.001)
    now=time.perf_counter()
    print(now*1000)

'''

and conclusion i take from this code is ;

29.2068
44.878
60.378800000000005
75.90679999999999
91.5121
107.0237
122.6039
138.3594
...
2950.1692
2965.7764
2981.4248000000002
2997.0353
3012.6643

as you can see it checks in almost every 15ms how could i fix this?

1 Answers1

0

The problem here is that the code inside the loop takes time. sleep will take 1 ms, then the call to perf counter takes up ? ms and the call to print() also. To be precise and do something exactly every 3 ms you should use a Timer and call your 'check' function upon a timer event.

Note that the job you want to run can take more time than 3 ms to execute. In this case you should use multithreading to ensure every check completes without being interrupted by a timer event

Here is an example of how to run code every n seconds

snus74
  • 406
  • 3
  • 9