【问题标题】:Python 3 Schedule runs at anytimePython 3 Schedule 随时运行
【发布时间】:2021-01-06 10:29:53
【问题描述】:

我目前正在编写一个可以启动多个任务的 Python 程序。对于时间管理,我已经安装了库 Schedule。不幸的是,调度程序每次都执行我的函数,尽管它没有正确的时间。不管时间是过去还是未来,函数总是被执行。

import time
from datetime import date
import schedule


class trader():
  def __init__(self):
      pass

  def scheduler(self, x, y, z):

    
    #schedule.every().monday.at("23:10").tag().do(self.daily(x, y, z))
    schedule.every().day.at("22:10").do(self.daily(x, y, z))

    while True:
        schedule.run_pending()
        time.sleep(1)

  def daily(self, x, y, z):
    
    print("running")

    
    return

感谢您的帮助

【问题讨论】:

    标签: python cron scheduled-tasks schedule


    【解决方案1】:

    Job.do() method 需要一个函数对象,但您正在调用一个函数并将结果传递给 .do()

    FAQ on schedule's documentation website 解释了如何将参数传递给计划函数。

    def my_job():
        # This job will execute every 5 to 10 seconds.
        print('Foo')
    
    schedule.every(5).to(10).seconds.do(my_job)
    

    OP的代码可以改成

    schedule.every().day.at("22:10").do(self.daily, x, y, z)
    

    【讨论】:

    • 感谢您的快速回答,给我自己的脸
    猜你喜欢
    • 1970-01-01
    • 2016-05-14
    • 2020-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-07
    • 1970-01-01
    相关资源
    最近更新 更多