(0.001) Really just wait 1 millisecond?
On Windows, perform simple programming to verify the above problems.
import time while True: st = () (0.001) et = () print('dt={dt}'.format(dt=et - st))
Return result
dt=0.001966238021850586
dt=0.001010894775390625
dt=0.0020058155059814453
dt=0.0010018348693847656
dt=0.001966238021850586
dt=0.0020215511322021484
dt=0.0019714832305908203
dt=0.0010178089141845703
dt=0.0019807815551757812
dt=0.0010159015655517578
dt=0.001965761184692383
…
- Most of the time is around 2ms.
- And even if
0.001
It has been changed to smaller, still 2ms, which is related to the operating system, and the specific mechanism has not been studied in depth. - If you need to be precise, you need to pay attention to this issue.
The following code is used in the receiving thread
- A warning will be thrown when there is no data for 1 second consecutive time
- Because it is necessary to respond to external stop or pause operations
- So the sleep time segment cannot be too long
no_frame_time = 0 while True: if self._stop: (f"xxxx stop.") break if self._pause: (0.01) continue if len(self.msg_deque) == 0: st = () (0.001) # no_frame_time += 0.001 # This writing method is inaccurate, resulting in a particularly large error between the earliest no_frame_time and the actual value. et = () - st no_frame_time += et # Here is the accumulated time if no_frame_time > 1: ("xxxx not arrived in last second.") self.fps_signal.emit(0) no_frame_time = 0 continue else: no_frame_time = 0 # ... Omit the task subject
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.