调度Python脚本以准确运行每小时

编程入门 行业动态 更新时间:2024-10-11 23:18:24
本文介绍了调度Python脚本以准确运行每小时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在我问之前, Cron工作和任务计划程序将是我的最后一个选项,这个脚本将用于Windows和Linux,我更喜欢有一个编码的方法,这到最终用户完成。

Before I ask, Cron Jobs and Task Scheduler will be my last options, this script will be used across Windows and Linux and I'd prefer to have a coded out method of doing this than leaving this to the end user to complete.

有没有一个Python库,我可以用来安排任务?我将需要每小时运行一次函数,但是,如果我每小时运行一次脚本一次,并使用.sleep,每小时一次将运行在从前一天的不同部分,由于延迟

Is there a library for Python that I can use to schedule tasks? I will need to run a function once every hour, however, over time if I run a script once every hour and use .sleep, "once every hour" will run at a different part of the hour from the previous day due to the delay inherent to executing/running the script and/or function.

最好的方法是计划函数在特定时间运行(不止一次) 使用Cron作业或使用Task Scheduler安排它?

What is the best way to schedule a function to run at a specific time of day (more than once) without using a Cron Job or scheduling it with Task Scheduler?

我也想要您的输入。 > b $ b 版本< 3.0

Or if this is not possible, I would like your input as well.

import datetime import time from apscheduler.scheduler import Scheduler # Start the scheduler sched = Scheduler() sched.daemonic = False sched.start() def job_function(): print("Hello World") print(datetime.datetime.now()) time.sleep(20) # Schedules job_function to be run once each minute sched.add_cron_job(job_function, minute='0-59')

out:

>Hello World >2014-03-28 09:44:00.016.492 >Hello World >2014-03-28 09:45:00.0.14110

版本> 3.0

(来自Animesh Pandey的回答如下)

Version > 3.0

(From Animesh Pandey's answer below)

from apscheduler.schedulers.blocking import BlockingScheduler sched = BlockingScheduler() @sched.scheduled_job('interval', seconds=10) def timed_job(): print('This job is run every 10 seconds.') @sched.scheduled_job('cron', day_of_week='mon-fri', hour=10) def scheduled_job(): print('This job is run every weekday at 10am.') sched.configure(options_from_ini_file) sched.start()

推荐答案

也许这可以帮助:高级Python调度程序

以下是其文档中的一小段代码:

Here's a small piece of code from their documentation:

from apscheduler.schedulers.blocking import BlockingScheduler def some_job(): print "Decorated job" scheduler = BlockingScheduler() scheduler.add_job(some_job, 'interval', hours=1) scheduler.start()

更多推荐

调度Python脚本以准确运行每小时

本文发布于:2023-11-23 19:21:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1622593.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:每小时   脚本   准确   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!