python计算写作业时间_Python每20秒运行一个cron作业

这个博客介绍了一个自定义的`ExtendedCrontab`类,它扩展了celery的crontab,增加了对秒级别的支持。通过这个类,可以更精确地控制定时任务的执行间隔。博客详细解释了类的实现,包括如何计算下一个执行时间的逻辑以及如何处理不同时间粒度的匹配。
摘要由CSDN通过智能技术生成

如果有人想在几秒钟内运行celeri的crontab,下面是一个通过扩展crontab的示例from celery.schedules import crontab, cronfield

from celery.utils.timeutils import ffwd

CRON_REPR = '''\

{0._orig_day_of_month} {0._orig_month_of_year} (s/m/h/d/dM/MY)>\

'''

class ExtendedCrontab(crontab):

'''

custom crontab to support 'seconds'

'''

def __init__(self, second=0, minute='*', hour='*', day_of_week='*',

day_of_month='*', month_of_year='*', nowfun=None, app=None):

super().__init__(second, minute, hour, day_of_week, day_of_month,

month_of_year, nowfun, app)

self._orig_second = cronfield(second)

self.second = self._expand_cronspec(second, 60)

def _delta_to_next(self, last_run_at, next_hour, next_minute, next_second):

'''

Takes a datetime of last run, next minute and hour, and

returns a relativedelta for the next scheduled day and time.

Only called when day_of_month and/or month_of_year cronspec

is specified to further limit scheduled task execution.

'''

_ffwd = super()._delta_to_next(last_run_at, next_hour, next_minute)

_ffwd.second = next_second

return _ffwd

def __repr__(self):

return CRON_REPR.format(self)

def __reduce__(self):

return (self.__class__, (self._orig_second,

self._orig_minute,

self._orig_hour,

self._orig_day_of_week,

self._orig_day_of_month,

self._orig_month_of_year), None)

def remaining_delta(self, last_run_at, tz=None, ffwd=ffwd):

tz = tz or self.tz

last_run_at = self.maybe_make_aware(last_run_at)

now = self.maybe_make_aware(self.now())

dow_num = last_run_at.isoweekday() % 7 # Sunday is day 0, not day 7

execute_this_date = (last_run_at.month in self.month_of_year and

last_run_at.day in self.day_of_month and

dow_num in self.day_of_week)

execute_this_hour = (execute_this_date and

last_run_at.day == now.day and

last_run_at.month == now.month and

last_run_at.year == now.year and

last_run_at.hour in self.hour and

last_run_at.minute < max(self.minute))

execute_this_minute = (last_run_at.minute in self.minute and

last_run_at.second < max(self.second))

if execute_this_minute:

next_second = min(second for second in self.second

if second > last_run_at.second)

delta = ffwd(second=next_second, microsecond=0)

else:

if execute_this_hour:

next_minute = min(minute for minute in self.minute

if minute > last_run_at.minute)

next_second = min(self.second)

delta = ffwd(minute=next_minute, second=next_second, microsecond=0)

else:

next_minute = min(self.minute)

next_second = min(self.second)

execute_today = (execute_this_date and

last_run_at.hour < max(self.hour))

if execute_today:

next_hour = min(hour for hour in self.hour

if hour > last_run_at.hour)

delta = ffwd(hour=next_hour, minute=next_minute,

second=next_second, microsecond=0)

else:

next_hour = min(self.hour)

all_dom_moy = (self._orig_day_of_month == '*' and

self._orig_month_of_year == '*')

if all_dom_moy:

next_day = min([day for day in self.day_of_week

if day > dow_num] or self.day_of_week)

add_week = next_day == dow_num

delta = ffwd(weeks=add_week and 1 or 0,

weekday=(next_day - 1) % 7,

hour=next_hour,

minute=next_minute,

second=next_second,

microsecond=0)

else:

delta = self._delta_to_next(last_run_at,

next_hour, next_minute,

next_second)

return self.to_local(last_run_at), delta, self.to_local(now)

def __eq__(self, other):

if isinstance(other, crontab):

return (other.month_of_year == self.month_of_year and

other.day_of_month == self.day_of_month and

other.day_of_week == self.day_of_week and

other.hour == self.hour and

other.minute == self.minute and

other.second == self.second)

return NotImplemented

ExtendedCrontab(second=second,

minute=minute,

hour=hour,

day_of_week=day_of_week,

day_of_month=day_of_month,

month_of_year=month_of_year)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值