python 每小时执行一次_调度Python脚本以准确运行每小时

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.

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.

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?

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

AP Scheduler fit my needs exactly.

Version < 3.0

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

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()

解决方案

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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值