Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用BlockingScheduler在5s上每10分钟运行一次任务?_Python_Cron - Fatal编程技术网

Python 如何使用BlockingScheduler在5s上每10分钟运行一次任务?

Python 如何使用BlockingScheduler在5s上每10分钟运行一次任务?,python,cron,Python,Cron,我试着每10分钟运行一次任务,例如在5点,13:15,13:25 然而,它不起作用 每小时只运行一次,在一小时开始时,从12点到下午4点 sched.add_job(运行批处理,'cron',周中的一天='mon-fri',小时='12-16',分钟='5,15,25,35,45,55',时区='America/Chicago')假设您希望代码运行周一到周五和12-16小时,则需要从12:05开始设置10分钟的间隔。使用cron的字符串格式,代码将从5分钟开始运行,然后每次增加10分钟 sche

我试着每10分钟运行一次任务,例如在5点,13:15,13:25

然而,它不起作用

每小时只运行一次,在一小时开始时,从12点到下午4点


sched.add_job(运行批处理,'cron',周中的一天='mon-fri',小时='12-16',分钟='5,15,25,35,45,55',时区='America/Chicago')
假设您希望代码运行周一到周五和12-16小时,则需要从12:05开始设置10分钟的间隔。使用cron的字符串格式,代码将从5分钟开始运行,然后每次增加10分钟

sched.add_job(run_batch, 'cron', day_of_week='mon-fri', hour='12-16', minute='5-59/10', timezone='America/Chicago')

问题标题表示您正在使用Python库的BlockingScheduler函数--Advanced Python Scheduler()

根据您的问题,您希望在周一到周五的1200到1600之间在Python脚本中运行cron作业。您希望作业在12:05、12:15、12:25等时间运行

我还没有完全测试下面的答案,但我确实测试了1个小时(从今天早上8点开始),它在8:05、8:15、8:25、8:35、8:45、8:55和9:05正确启动

我使用start_date变量在精确的日期和时间启动调度程序

from datetime import datetime
from apscheduler.schedulers.background import BlockingScheduler

# BlockingScheduler: use when the scheduler is the only thing running in your process
scheduler = BlockingScheduler()

# Define the function that is to be executed
# at a scheduled time
def job_function():
    print(datetime.now().time().strftime('%H:%M'))

# Schedules the job_function to be executed Monday through Friday at between 12-16 at specific times.   
scheduler.add_job(job_function, 'cron', day_of_week='mon-fri', hour='12-16', minute='5,15,25,35,45,55', start_date='2021-03-23 12:00:00', timezone='America/Chicago')

# Start the scheduler
scheduler.start()

顺便说一句,您可以替换这些时间间隔

minute='5,15,25,35,45,55'
用这个,我测试过

minute='5-55/10'
只是个附带问题。您是否考虑过在中心时间使用UTC

from datetime import datetime
from apscheduler.schedulers.background import BlockingScheduler

# BlockingScheduler: use when the scheduler is the only thing running in your process
scheduler = BlockingScheduler()

# Define the function that is to be executed
# at a scheduled time
def job_function():
    print(datetime.utcnow().strftime('%H:%M'))

# Schedules the job_function to be executed Monday through Friday at between 12-16 at specific times.   
scheduler.add_job(job_function, 'cron', day_of_week='mon-fri', hour='17-21', minute='5-55/10', start_date='2021-03-23 12:00:00', timezone='UTC')

# Start the scheduler
scheduler.start()


如果您对此代码有任何问题,请告诉我,我将为您调查。

您的代码没有问题唯一的问题是您的系统时区不同,这使得代码在时间匹配时运行或只运行一次。因此,要解决这个问题,您需要使用
os.environ['TZ']='America/Chicago'
,并且它工作正常。我已经测试了两个小时,它在
5,15,25,35,45,55
上运行了两个小时

from apscheduler.schedulers.blocking import BlockingScheduler
import pytz
import os, time

os.environ['TZ'] = 'America/Chicago'
def run_batch():
    print("Hello World")
  

scheduler = BlockingScheduler()
scheduler.add_job(run_batch, 'cron' ,day_of_week='mon-fri', hour='12-16', minute='5,15,25,35,45,55', timezone='America/Chicago')
scheduler.start()

minute=10
使其每10分钟运行一次?是的,代码将每10分钟运行一次我在文档中的任何地方都看不到这一点。你有消息来源吗?是的,这是开始日期为什么时区很重要?它仍然会每五分钟运行一次,而不管我是否尝试过代码。我认为代码第一次正常运行取决于时区。但是,如果时区不同,时间也会不同,这可能超出了您的时间范围。出于某种原因,这似乎现在起作用了@CodyBugstein我总是觉得编码很吸引人,因为一个未知的事物可以把任何事情都带入混乱和质疑之中。