简体   繁体   English

在特定时间触发python脚本

[英]Fire a python script at a specific time

Im attempting to build a sort of job system in python. 我正在尝试在python中建立某种工作系统。 This consits of a front end web page that allows people to schedule jobs to be run at x time. 这是一个前端网页的组成部分,该页面允许人们安排在x时间运行的作业。 The PHP then writes the job including it's run time to a database. 然后,PHP将包含运行时间的作业写入数据库。 I Then have a python cronjob running on my server that will check in every 30 mins to see if there is a new job. 然后,我在服务器上运行一个python cronjob,它将每30分钟检查一次,以查看是否有新工作。 If there is, it will fire a schedule script to count down to the exact minute of the event/job before firing. 如果有的话,它将在启动前触发时间表脚本以倒数到事件/作业的确切分钟。

The script im using for the scheduler can be found at: https://github.com/dbader/schedule . 用于调度程序的脚本im可以在以下网址找到: https : //github.com/dbader/schedule It seems like a pretty nice script and i found it on another SO page. 似乎是一个非常不错的脚本,我在另一个SO页面上找到了它。 However - after installing via PIP, the module doesnt seem to be working? 但是-通过PIP安装后,该模块似乎无法正常工作?

Attempting to run a schedule script - the Python engine crashes on: schedule.today.at('14:38').do(job) I simply need to run a one time scheduler that can be reused to schedule multiple jobs at the same time. 尝试运行计划脚本-Python引擎崩溃:schedule.today.at('14:38')。do(job)我只需要运行一个一次性的计划程序即可重复使用,以同时计划多个作业时间。 There is a small chance these jobs might repeat same time next week, hence i like the schedule module. 这些工作下周可能会重复一次,因此我很喜欢schedule模块。

This is my example code built from a few snippets found on the GIT: 这是我的示例代码,它是根据GIT上的一些代码片段构建的:

import schedule
import time

def job():
    # Do some work ...

    print("Hello World")
    return schedule.CancelJob

#schedule.every(10).seconds.do(job)
schedule.today.at('14:38').do(job)

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

If anyone else is having the same problem - or has an alternative idea on how to achieve this - please let me know!! 如果其他人遇到相同的问题-或对如何实现此问题有其他想法-请让我知道!

Looking through the source code on the master branch, schedule does not expose an attribute called today . 在master分支上查看源代码时, schedule不会公开一个名为today的属性。 If you've seen an example that used such an attribute than the issue would seem to be that you are on a version of the library from which it has been removed. 如果您看到了使用此类属性的示例,那么问题似乎在于您所使用的是该库的版本。

Unfortunately, this library doesn't look like a good fit if your main requirement is the ability to schedule one off tasks. 不幸的是,如果您的主要要求是计划一项完成任务的能力,那么该库似乎不合适。

Edit : 编辑

There is no today attribute so the snippet mentioned in the FAQ won't work directly but you should be able to salvage the basic idea of it: today没有属性,因此常见问题解答中提到的代码段无法直接使用,但您应该可以挽救它的基本思想:

def job_that_executes_once():
    # Do some work ...
    return schedule.CancelJob

schedule.every().day().at('14:38').do(job_that_executes_once)

Edit 2 : 编辑2

Possibly useful: an "only do this job once" decorator. 可能有用:“仅执行一次此工作”装饰器。

from functools import wraps

import schedule


def do_once(func):
    @wraps(func)
    def wrapped(*args, **kwargs):
        func(*args, **kwargs)
        return schedule.CancelJob
    return wrapped

@do_once
def some_one_off_job():
    # Do some work
    # No need to return schedule.CancelJob
    print 5

@do_once
def some_other_one_off_job():
    # hooray, decorators!
    pass

schedule.every().day().at('14:38').do(some_one_off_job)
schedule.every().day().at('14:38').do(some_other_one_off_job)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM