Twisted 中的每个刻度运行一个函数

发布于 2024-09-10 06:36:52 字数 117 浏览 1 评论 0原文

我正在使用扭曲框架,我需要跟踪自事件开始以来已经过去了多长时间,并在经过一定时间后执行操作。

在我看来,最好的方法是检查反应堆每次滴答的时间戳。如果这是最好的方法,我该怎么做?如果不是,有什么更好的方法?

I'm using the twisted framework, and I need to keep track of how much time has passed since an event has started, and perform an action when a certain amount has passed.

The best way to do that seems to me to be to check against a time-stamp each tick of the reactor. If it is the best way, how do I do it? If it isn't, what's a better way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南城追梦 2024-09-17 06:36:52

您想要使用 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()

(我不认为反应堆里真的有“蜱虫”之类的东西,至少,不是我猜你的意思。)

You want to use callLater.

Here's a complete, runnable example which does what you are asking, "perform an action when a certain amount (of time) has passed since an event has started".

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()

(I don't think there's really any such thing as a 'tick' in the reactor, at least, not in the sense that I'm guessing you mean.)

初心未许 2024-09-17 06:36:52

我正在寻找的功能似乎在这里描述:
在扭曲协议中定期运行函数

这是我的代码:

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)

The functionality I was looking for seems to be described here:
Running a function periodically in twisted protocol

Here's my code:

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文