如何在完成后取消预定作业 -Python
How to cancel scheduled job once it's completed -Python
import schedule
import time
def test():
print("Stopped")
# I tried these two commands below it's not helping or maybe I am doing it wrong.
# schedule.cancel_job(test)
# schedule.clear()
schedule.every().day.at("21:43").do(test)
while True:
schedule.run_pending()
time.sleep(1)
我想在作业完成后取消它,在这里我知道它没有完成,因为我已经使用了 day 所以它每天运行,但我只想在执行完这项工作后取消它,或者如果有任何其他方法可以做同样的事情,那会很好,而且必须像计划一样容易。
从技术上讲,我已经创建了一个 python 机器人,有多个计划任务我想在那里实现这个想法。
另外,如果它与我的查询有关,我不理解:
how to cancel python schedule
如果您只希望它 运行 一次,请将行 return schedule.CancelJob
添加到函数的末尾:
import schedule
import time
def test():
print("Stopped")
return schedule.CancelJob
schedule.every().day.at("21:43").do(test)
while True:
schedule.run_pending()
time.sleep(1)
如果你想打破循环:
import schedule
import time
running = True
def test():
global running
running = False
print("Stopped")
return schedule.CancelJob
schedule.every().day.at("21:43").do(test)
while running:
schedule.run_pending()
time.sleep(1)
听起来您只希望计划任务 运行 一次。
如果是这样,这是日程安排网页上的常见问题解答,它给出了以下建议
def job_that_executes_once():
# Do some work ...
return schedule.CancelJob
schedule.every().day.at('22:30').do(job_that_executes_once)
见https://schedule.readthedocs.io/en/stable/faq.html#how-can-i-run-a-job-only-once
在所有作业完成或取消后退出程序的更好方法如下
import schedule
import time
def test():
print("Running")
return schedule.CancelJob
schedule.every().minute.do(test)
while True:
schedule.run_pending()
if not schedule.jobs:
break
time.sleep(1)
print("I'm done")
当调度列表中没有更多作业时,此代码将跳出 while 循环。
python schedule
库似乎用于定期作业。您所描述的更多是时间触发的工作。你最好在 while True
循环中检查时间。
顺便说一句,如果您标记了您的工作,您可以使用schedule.cancel_job(标签)取消您的工作。
import schedule
import time
def test():
print("Stopped")
# I tried these two commands below it's not helping or maybe I am doing it wrong.
# schedule.cancel_job(test)
# schedule.clear()
schedule.every().day.at("21:43").do(test)
while True:
schedule.run_pending()
time.sleep(1)
我想在作业完成后取消它,在这里我知道它没有完成,因为我已经使用了 day 所以它每天运行,但我只想在执行完这项工作后取消它,或者如果有任何其他方法可以做同样的事情,那会很好,而且必须像计划一样容易。 从技术上讲,我已经创建了一个 python 机器人,有多个计划任务我想在那里实现这个想法。 另外,如果它与我的查询有关,我不理解:
how to cancel python schedule
如果您只希望它 运行 一次,请将行 return schedule.CancelJob
添加到函数的末尾:
import schedule
import time
def test():
print("Stopped")
return schedule.CancelJob
schedule.every().day.at("21:43").do(test)
while True:
schedule.run_pending()
time.sleep(1)
如果你想打破循环:
import schedule
import time
running = True
def test():
global running
running = False
print("Stopped")
return schedule.CancelJob
schedule.every().day.at("21:43").do(test)
while running:
schedule.run_pending()
time.sleep(1)
听起来您只希望计划任务 运行 一次。
如果是这样,这是日程安排网页上的常见问题解答,它给出了以下建议
def job_that_executes_once():
# Do some work ...
return schedule.CancelJob
schedule.every().day.at('22:30').do(job_that_executes_once)
见https://schedule.readthedocs.io/en/stable/faq.html#how-can-i-run-a-job-only-once
在所有作业完成或取消后退出程序的更好方法如下
import schedule
import time
def test():
print("Running")
return schedule.CancelJob
schedule.every().minute.do(test)
while True:
schedule.run_pending()
if not schedule.jobs:
break
time.sleep(1)
print("I'm done")
当调度列表中没有更多作业时,此代码将跳出 while 循环。
python schedule
库似乎用于定期作业。您所描述的更多是时间触发的工作。你最好在 while True
循环中检查时间。
顺便说一句,如果您标记了您的工作,您可以使用schedule.cancel_job(标签)取消您的工作。