(python)每隔x秒执行一次函数

2024-06-28 19:32:17 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的源代码,每1秒执行一次函数

买,它只执行一次。你知道吗

简而言之,我想每秒钟从指定的管道输出中获取#

对这个问题有什么建议吗?你知道吗

谢谢你的帮助

class Monitor:
    def __init__(self, namedpipe, name):
        self.namedpipe = namedpipe
        self.name = name
        self.count = 0
    def run(self):
        print self.name+" start run"
        while True:
            line = self.namedpipe.readline()
            self.count = self.count+1
    def getCost(self):
        print "Hi"


while True:
    line = monitor.readline()
    line = line.strip()
    if not line: 
        ans =raw_input('stop?')
        if ans=='y': break
    monitorList = open(line, 'r')
    m = Monitor(monitorList, line)
    thread.start_new_thread(m.run, ())
    time.sleep(1)
    threading.Timer(0.1, m.getCost).start()

Tags: runnameselftruereadlineifdefcount
1条回答
网友
1楼 · 发布于 2024-06-28 19:32:17

与线程计时器,您必须在计时器每次过期时重新启动它—因此在执行计时器过期时调用的函数期间—代码中的m.getCost—必须再次启动计时器。您可能需要定义一个包装函数来完成这项工作,以避免用计时器的东西污染m.getCost()。你知道吗

相关问题 更多 >