我正在编写一个代码,我想每隔n秒运行一次函数。
创建不同的类对象以调用此函数。
我的代码是这样的:
class MTR:
def __init__(self, serverIP, destinationIP):
self.serverIP= serverIP
self.destinationIP= destinationIP
def get_mtr_result(self):
#ssh to server IP and run mtr to destination ip
def recur_code(self):
threading.Timer(5.0, self.recur_code).start()
#get the output from get_mtr_result() and write it to a file every 5 sec
if __name__== "__main__":
pool_size= 5
pool= Pool(pool_size)
serverIPList= ["1.1.1.1", "2.2.2.2", "3.3.3.3"]
objects= [MTR(server_ip, destination_ip) for server_ip in serverIPList]
for obj in objects:
pool.apply_async(obj.recur_code, ())
pool.close()
pool.join()
代码完美无缺,但会自动停止。除非键盘中断,否则我不希望代码停止。我假设这与for循环和pool.close()有关,但我无法弄明白。任何帮助将不胜感激。
谢谢!