Python 3 计划随时运行

Python 3 Schedule runs at anytime

我目前正在编写一个 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

感谢您的帮助

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)