x分ごとに与えられたメソッドの実行をプログラムする必要があります。x分ごとに関数を実行するにはschedまたはthreading.Timer?
私はそれを行うための2つの方法を見つけました:最初はsched
モジュールを使用し、2番目はThreading.Timer
を使用しています。
最初の方法:
import sched, time
s = sched.scheduler(time.time, time.sleep)
def do_something(sc):
print "Doing stuff..."
# do your stuff
sc.enter(60, 1, do_something, (sc,))
s.enter(60, 1, do_something, (s,))
s.run()
二:
import threading
def do_something(sc):
print "Doing stuff..."
# do your stuff
t = threading.Timer(0.5,do_something).start()
do_something(sc)
何の違いだとその1、他よりも優れたものがある場合は?
これは 'asyncio'でも行うことができますが、私はそのモジュールでは(まだ)良くありません。 – DainDwarf