如何取消计划的作业,一旦它';s已完成-Python

如何取消计划的作业,一旦它';s已完成-Python,python,schedule,Python,Schedule,我想在作业完成后取消它,在这里我知道它没有完成,因为我使用了天,所以它每天都运行,但我只想在作业完成后取消它,或者如果有其他方法来做同样的事情,这将是很好的,它必须像计划一样简单。 从技术上讲,我已经创建了一个python机器人,我想在那里实现这个想法,有多个预定任务。 另外,如果这与我的问题有关,我不理解: 如果您只希望它运行一次,请在函数末尾添加行返回计划。CancelJob: import schedule import time def test(): print("S

我想在作业完成后取消它,在这里我知道它没有完成,因为我使用了,所以它每天都运行,但我只想在作业完成后取消它,或者如果有其他方法来做同样的事情,这将是很好的,它必须像计划一样简单。 从技术上讲,我已经创建了一个python机器人,我想在那里实现这个想法,有多个预定任务。 另外,如果这与我的问题有关,我不理解:


如果您只希望它运行一次,请在函数末尾添加行
返回计划。CancelJob

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)
如果要中断循环,请执行以下操作:

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)

如果您只希望它运行一次,请在函数末尾添加行
返回计划。CancelJob

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)
如果要中断循环,请执行以下操作:

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)

当计划列表中没有更多作业时,此代码将中断while循环。

听起来您只希望计划任务运行一次

如果是这样,这是日程网页上的常见问题,并给出以下建议

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)

当计划列表中没有更多作业时,此代码将中断while循环。

python
计划库似乎是用于定期作业的。你所描述的更多的是一个时间触发的工作。您最好在
中检查时间,而在True
循环中检查时间


顺便说一下,如果您标记了作业,您可以使用schedule.cancel_job(标记)来取消作业。

python
计划库似乎适用于定期作业。你所描述的更多的是一个时间触发的工作。您最好在
中检查时间,而在True
循环中检查时间


顺便说一句,如果你标记了你的工作,你可以使用日程表。cancel_job(标记)来取消你的工作。

比我快几秒钟!如果您将链接添加到文档中,我很乐意删除我的答案。程序在打印“已停止”后没有停止,我希望程序停止,因为作业已完成。请参阅我刚才编辑的“如果要中断循环”代码块。这应该解决你的问题problem@VishwasLuhana我在回答中提出了另一种退出循环的方法。比我快几秒钟!如果您将链接添加到文档中,我很乐意删除我的答案。程序在打印“已停止”后没有停止,我希望程序停止,因为作业已完成。请参阅我刚才编辑的“如果要中断循环”代码块。这应该解决你的问题problem@VishwasLuhana我在回答中提出了退出循环的另一种方法。你能用例子回答吗?你能用例子回答吗?程序在完成作业后不会停止。@VishwasLuhana你没有要求这个。。。您希望任务只安排一次。如果要结束程序,您需要做一些事情来打破while循环。@VishwasLuhana我已经编辑了我的答案以包含此功能。程序在完成作业后不会停止。@VishwasLuhana您没有要求这样做。。。您希望任务只安排一次。如果你想结束程序,你需要做一些事情来打破while循环。@VishwasLuhana我已经编辑了我的答案以包含此功能。