Reputation: 11
I have been trying to schedule actions in Python, using a for loop to iterate through an SQL query. I only get scheduled the last iteration.
con = mdb.connect(dbhost, dbuser, dbpass, dbname)
cur = con.cursor()
cur.execute("""SELECT * FROM `jobs`""")
rows = cur.fetchall()
for row in rows:
status = row[1]
names = row[2]
script = row[3]
times = row[4]
output = row[5]
output_s = row[6]
print(names, status, output_s)
def job():
if status == 1 and output_s == 1:
system = os.popen(script)
system_output = system.read()
print(system_output)
cur.execute("""UPDATE jobs SET output = %s WHERE `name` = %s""", [system_output, names])
con.commit()
elif status == 1 and output_s == 0:
system = os.popen(script)
cur.execute("""UPDATE jobs SET output = "Output Disabled" WHERE `name` = %s""", [names])
con.commit()
print('output disabled')
else:
print('job disabled')
schedule.every(times).seconds.do(job)
while True:
schedule.run_pending()
time.sleep(1)
Upvotes: 1
Views: 543
Reputation: 1862
Just indent schedule.every(times).seconds.do(job)
to be in the for loop.
Currently it is outside the for loop (see that it isn't indented like the other lines in the loop), so it is only ran after the loop finishes all iterations.
Upvotes: 2