安排for循环

时间:2020-10-23 13:07:23

标签: python schedule

我一直在尝试使用for循环遍历SQL查询来计划Python中的动作。我只计划最后一次迭代。

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)

1 个答案:

答案 0 :(得分:2)

只需缩进schedule.every(times).seconds.do(job)就可以进入for循环。

当前它在for循环之外(请注意,它不像循环中的其他行那样缩进),因此仅在循环完成所有迭代之后才运行。