Sleep for exact time in python

前端 未结 7 1483
迷失自我
迷失自我 2020-12-11 17:17

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

7条回答
  •  南笙
    南笙 (楼主)
    2020-12-11 17:40

    Because you're working with a preemptive operating system, there's no way you can guarantee that your process will be able to have control of the CPU in 25ms.

    If you'd still like to try, it would be better to have a busy loop that polls until 25ms has passed. Something like this might work:

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

提交回复
热议问题