Pythonで:xでの間隔:00繰り返します
質問
5分間隔でリピートタイマーをスケジュールするにはどうすればよいですか。 00秒で発砲し、00で繰り返します。OK、ハードリアルタイムではなく、SYSラグでできるだけ近い。ラグでの蓄積を避け、00近くになるようにしようとしています。
Lang:Python、OS:winxp x64
システムには25msの解像度があります。
すべてのコードが役立つでしょう、TIA
解決
私はそれをより正確に行う方法を知りません Threading.timer. 。それは「ワンショット」ですが、それはあなたがスケジュールする関数を、そのようにスケジュールする関数を、すぐに300秒後に再スケジュールする必要があることを意味します。 (正確な時間をで測定することにより、精度を追加できます time.time
それに応じて次のスケジューリング遅延を変更するたびに)。
他のヒント
これら2つのコードサンプルのタイムプリントアウトを試してみてください。
コードサンプル1
import time
delay = 5
while True:
now = time.time()
print time.strftime("%H:%M:%S", time.localtime(now))
# As you will observe, this will take about 2 seconds,
# making the loop iterate every 5 + 2 seconds or so.
## repeat 5000 times
for i in range(5000):
sum(range(10000))
# This will sleep for 5 more seconds
time.sleep(delay)
コードサンプル2
import time
delay = 5
while True:
now = time.time()
print time.strftime("%H:%M:%S", time.localtime(now))
# As you will observe, this will take about 2 seconds,
# but the loop will iterate every 5 seconds because code
# execution time was accounted for.
## repeat 5000 times
for i in range(5000):
sum(range(10000))
# This will sleep for as long as it takes to get to the
# next 5-second mark
time.sleep(delay - (time.time() - now))
所属していません StackOverflow