我有一个程序可以编写xml并写入其他文件,但是s a thing that I want to do and I don
不知道如何,我有一个检查启动文件夹中是否包含某些文件的功能,以及每分钟运行的其他功能,我想让该功能检查文件是否存在30到30分钟,而又不停止其他功能每分钟运行一次,该怎么办?
#this one needs to run from 30 to 30 minutes
def modifica_xml(self):
global xmlformatado
self.IV_config.read('config.ini')
MakerStudio = self.IV_config['config']['MakerStudio'] + '\sntlconfig.xml'
print(MakerStudio)
Boostrap = self.IV_config['config']['Bootstrap'] + '\sntlconfig.xml'
print(Boostrap)
xmlconfig = '''<?xml version="1.0"?>
<SentinelConfiguration><SentinelKeys><Protocol>SP_TCP_PROTOCOL
</Protocol><ContactServer>{}</ContactServer><Heartbeat>60</Heartbeat>
</SentinelKeys></SentinelConfiguration>'''
try:
xmlformatado = xmlconfig.format(self.IV_linksFuncionando[0])
except IndexError:
pass
if os.path.exists(self.IV_arquivotxt):
self.IV_autodestruct = len(open(self.IV_arquivotxt).readlines())
if self.IV_autodestruct >= 180:
os.remove(self.IV_arquivotxt)
print("Arquivo excluido apos 3 horas")
else:
pass
try:
print('Utilizando ' + self.IV_linksFuncionando[0])
with open(Boostrap, 'w') as f:
f.write(xmlconfig.format(self.IV_linksFuncionando[0]))
f.close()
except IndexError:
print("Ambos servidores estao offline")
except PermissionError:
pass
except FileNotFoundError:
pass
try:
print('Utilizando ' + self.IV_linksFuncionando[0])
with open(MakerStudio, 'w') as f:
f.write(xmlconfig.format(self.IV_linksFuncionando[0]))
f.close()
except IndexError:
print("Ambos servidores estao offline")
except PermissionError:
pass
except FileNotFoundError:
pass
def escreve_txt(self):
try:
with open(self.IV_arquivotxt, 'a') as x:
x.write('Licença em uso ' + str(self.IV_linksFuncionando[0]) + ' ' + str(datetime.now().strftime("%d/%m/%Y, %H:%M:%S")) + '\n')
x.close()
except IndexError:
with open(self.IV_arquivotxt, 'a') as x:
x.write('Ambos servidores offline ' + str(datetime.now().strftime("%d/%m/%Y, %H:%M:%S")))
x.close()
答案 0 :(得分:1)
您可以为下次调用该函数设置时间戳:
import time
next_1min = 0
next_30min = 0
while True:
ts = time.time()
if ts > next_1min:
next_1min = ts + 60
your_1min_f()
if ts > next_30min:
next_30min = ts + 30*60
your_30min_f()
,如果您希望功能以固定的间隔运行(例如9h30、10h00、10h30 ...) 您可以将增量设置为:
next_30min = ts - ts % (30*60) + 30*60
如果需要精确地同时调用函数,则必须使用某种并行性,例如使用线程模块:
import time
import threading
next_1min = 0
next_30min = 0
while True:
ts = time.time()
if ts > next_1min:
next_1min = ts - ts % 60 + 60
threading.Thread(target= your_1min_f).start()
if ts > next_30min:
next_30min = ts - ts % (30*60) + 30*60
threading.Thread(target= your_30min_f).start()