I know that win32api.Sleep
is related to the windows API, and it is obvious that Python’s default time.sleep
param is in seconds, and win32api.Sleep
is in milliseconds, other than that is there a difference between time.sleep
, and win32api.Sleep
? I like the idea that win32api.Sleep
is in milliseconds and I’m considering using it over time.sleep
.
The obvious difference is that win32api.Sleep
is not portable while time.sleep
is. If you’re certain that your program will never need to run on a non-windows system this doesn’t matter, but it’s generally a good idea to try to write as portable code as possible. You never know what the future holds.
time.sleep takes a float and can do millisecond sleeps or shorter sleeps, depending on the OS implementation.
time.sleep(0.001)
Another difference is that win32api.Sleep
accepts a boolean parameter that allows it to return early in some cases if you’re also using Windows-specific functions for I/O. From Microsoft’s documentation:
If the parameter is TRUE and the thread that called this function is the same thread that called the extended I/O function (ReadFileEx or WriteFileEx), the function returns when either the time-out period has elapsed or when an I/O completion callback function occurs. If an I/O completion callback occurs, the I/O completion function is called. If an APC is queued to the thread (QueueUserAPC), the function returns when either the time-out period has elapsed or when the APC function is called.
If you don’t need that feature, it’s probably better to use time.sleep
, as it’s platform-independent and included in the standard library, whereas the win32api
module is part of the third-party pywin32 package.