Python time.sleep vs busy wait accuracy

前端 未结 2 1809
难免孤独
难免孤独 2021-01-12 00:42

I was playing around with the time.sleep function from python\'s standard library and found it inadequate for sub-ms delays. From testing I found it to actually wait 1.1-1.2

2条回答
  •  太阳男子
    2021-01-12 01:43

    This is sort of a duplicate question, but I'll try to answer it here anyway to the best of my ability.

    The sleep function is an OS call that differs from busy wait in that it doesn't block the thread. If you have a multi-threaded script though, it shouldn't block the other threads. The sleep function is inaccurate in Windows because it is not a realtime OS (not sure what that means). If you are looking strictly for accuracy of the wait, busy wait is the way to go. Otherwise, time.sleep() is probably preferred. The reason the OS call to sleep is inaccurate is probably because it relies on the OS to return at the correct time, and is reliant on the precision of the OS's scheduler.

提交回复
热议问题