Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>time.sleep(N)</code> attempts to sleep <em>at least</em> N seconds of elapsed, AKA "wall-clock" time - of course there can be no guarantee that the sleep will last <em>exactly</em> N seconds; for example, the thread becomes ready to execute again at that time, but it cannot necessarily preempt whatever other thread is executing at that time -- that's the operating system's decision to make, not any programming language's; on the other hand, sleep may be prematurely interrupted by various kinds of events (such as interrupts).</p> <p>If you can find on your operating system some clock-like thingy that only advances when the system's state is the one you care about (e.g. "not hybernated", in your case), then of course you can go back to sleep if you wake up again "too early".</p> <p>For example, on Windows 7, <a href="http://msdn.microsoft.com/en-us/library/ee662307(VS.85).aspx" rel="noreferrer">QueryUnbiasedInterruptTime</a> is specifically documented to "not include time the system spends in sleep or hibernation" and to use units of 100 nanoseconds. So if you call that, e.g. through <code>ctypes</code>, you can achieve the effect you want:</p> <pre><code>def unbiasedsleep(n): start = kernel32.QueryUnbiasedInterruptTime() target = start + n * 10 * 1000 * 1000 while True: timeleft = target - kernel32.QueryUnbiasedInterruptTime() if timeleft &gt; 0: time.sleep(timeleft / (10 * 1000 * 1000.0)) </code></pre> <p>I don't know how to get the equivalent of QueryUnbiasedInterruptTime on other releases of Windows or other operating systems, but then, you don't tell us what operating system(s) you're interested in, so it would be pretty pointless anyway to present a long laundry lists of approaches which may work similarly in different environments.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload