Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在指定时间执行函数_Python - Fatal编程技术网

Python 在指定时间执行函数

Python 在指定时间执行函数,python,Python,我想每天12点执行一些代码。我唯一能想到的办法就是 睡到12点 执行代码 24小时睡眠(减去运行代码所需的时间) 转到2 有没有一个内置的API可以用来安排这样的事件 我不能使用cron作业或任何类似的东西,这必须是python脚本,希望这对您有用 import threading def latercomer(): print "Oops I am Late" t = threading.Timer(30.0, latercomer) t.start() 迟到者将在30秒后接到电话

我想每天12点执行一些代码。我唯一能想到的办法就是

  • 睡到12点
  • 执行代码
  • 24小时睡眠(减去运行代码所需的时间)
  • 转到2
  • 有没有一个内置的API可以用来安排这样的事件


    我不能使用cron作业或任何类似的东西,这必须是python脚本,希望这对您有用

    import threading
    def latercomer():
        print "Oops I am Late"
    
    t = threading.Timer(30.0, latercomer)
    t.start()
    
    迟到者将在30秒后接到电话

    我认为这对你的情况很有用

    pip install schedule
    
    下面是文档中的代码片段:

    import schedule
    import time
    
    def job():
        print("I'm working...")
    
    schedule.every(10).minutes.do(job)
    schedule.every().hour.do(job)
    schedule.every().day.at("10:30").do(job)
    schedule.every(5).to(10).minutes.do(job)
    schedule.every().monday.do(job)
    schedule.every().wednesday.at("13:15").do(job)
    schedule.every().minute.at(":17").do(job)
    
    while True:
        schedule.run_pending()
        time.sleep(1)
    

    如果我想让它在某个时间运行呢?不确定间隔?