multithreading - how to solve "when python script is running in background, the time.sleep() performs very bad?" -


i'm using wxpython , threading module of python build simple timer gui. used time.slee(0.5) measure time. problem is: once start timer , switch other program chrome. times later when switched back, time displayed on timer inaccurate. example, if set 3 minutes run, turns out timer 3 times slower operating system clock.

after searching, know time.sleep() unreliable, seems gap huge makes me doubt if there wrong.

i noticed if don't put script background, there 2-3 seconds difference.

i can accept several seconds difference, not 2 "minutes".how can solve it?

sleep() not guarantee scheduling after time given. guarantees sleep at least time. can sleep longer, depending on system load.

just compare against system time time.time (or time.clock, depends on os - read notes in documentation). after wakeup , use sleep() not occupy cpu time.

edit:

there several ways improve that: - if timer has wakeup after time elapsed, sleep() until short before wakeup , busy-wait time elapse. block gui/app during timeout. not good.

  • you can use threading 1 thread display update @ ca. 1s (or whatever think appropriate) , thread timeout shown above. bit complicated.

    • a third approach use sleep() gui-update without busy-wait each second. give bitt less accurate display, less cpu performance. short before time elapsed, busy wait time remaining.

ok, voluntary consulting.


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -