微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

安排for循环

如何解决安排for循环

我一直在尝试使用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)

解决方法

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。