ツイストフレームワークを使用しているので、イベントが開始されてからどれくらいの時間が経過したかを追跡し、一定の時間が経過したときにアクションを実行する必要があります。
それを行うための最良の方法は、原子炉の各ティックのタイムスタンプをチェックすることだと私には思えます。それが最善の方法である場合、どうすればよいですか?そうでない場合、より良い方法は何ですか?
使用したいcallLater
。
これは、「イベントが開始されてから一定の時間(時間)が経過したときにアクションを実行する」という、あなたが求めていることを実行する完全な実行可能な例です。
from twisted.internet import reactor
certainAmount = 0.73 # this is in seconds
def startedEvent():
print 'started event'
reactor.callLater(certainAmount, performAnAction)
def performAnAction():
print 'performed an action'
reactor.stop()
startedEvent()
reactor.run()
(少なくとも、私があなたが言っていると推測しているという意味では、原子炉に「ダニ」のようなものは実際にはないと思います。)
私が探していた機能はここで説明されているようです: ツイストプロトコルで定期的に関数を実行する
これが私のコードです:
def check_time(self):
for game in self.games:
if self.games[game]['state'] == 'GAME':
game_start_time = self.games[game]['starttime']
if game_start_time is None:
continue
elif game_start_time + 300 > time.time():
#300 seconds = 5 minutes.
continue
else:
self.end_game(game)
def __init__(self):
self.timecheck = task.LoopingCall(self.check_time)
self.timecheck.start(1)