Reputation: 42
I have this program that writes an xml and write some other files, but theres a thing that I want to do and I don
t know how, I have a function that checks if some files are in the startup folder, and other functions that run every minute, I want to make the function to check if files are there from 30 to 30 minutes, without stopping the other functions to run minute by minute, how can I do that?
#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()
Upvotes: 0
Views: 43
Reputation: 308
you can set a timestamp for the next time the function should be called:
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()
and if you want the functions to run at fixed intervals (say 9h30, 10h00, 10h30...) you can set the increment to:
next_30min = ts - ts % (30*60) + 30*60
and in case you need to call the functions exactly at the same time then you have to use some kind of parallelism, for example using the threading module:
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()
Upvotes: 1