[英](python) execute function every x seconds
這是我每 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()
使用 Threading.Timer,您必須在每次計時器到期時重新啟動計時器 - 因此在計時器到期時調用的函數的執行期間 - 代碼中的 m.getCost - 計時器必須再次啟動。 您可能想要定義一個包裝函數來完成這項工作,以避免使用計時器的東西污染 m.getCost()。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.