Apscheduler学习笔记之job

更新时间:2023-07-12 04:11:27 阅读: 评论:0

Apscheduler学习笔记之job
下⾯是job.py源代码,做了⼀点修改。
这是我第⼀篇⽂章,请⼤家⽀持。
主要属性:
1. ⼀个job(有唯⼀id)就是在trigger 的激发下执⾏func(args, kwargs)的任务. 具体执⾏由executor来做。
job 执⾏流程:时间读⼊ -> trigger 激发 -> executor执⾏job -> 计算下⼀次运⾏时间
2. ⼀个job会被某个scheduler安排,必要的话会被存储在某个jobstore⾥。下⼀次执⾏程序时候, scheduler会考虑jobstore的执⾏。
3. 初始化时,间接由⽅法_modify实现
次要属性:
4. 错过执⾏时间的处理
5. 下⼀次执⾏时间
6. 提供job名称
⽅法:
间接使⽤scheduler的⽅法,如 job.pau 就是调⽤lf._scheduler.pau_job(scheduler通过id, jobstore_alias查询job)
from collections import Iterable, Mapping
from uuid import uuid4
iggers.ba import BaTrigger
from apscheduler.util import (
ref_to_obj, obj_to_ref, datetime_repr, repr_escape, get_callable_name, check_callable_args,
convert_to_datetime)
class Job(object):
"""
Contains the options given when scheduling callables and its current schedule and other state.
This class should never be instantiated by the ur.
:var str id: the unique identifier of this job
:var str name: the description of this job
:var func: the callable to execute
:var tuple|list args: positional arguments to the callable
:var dict kwargs: keyword arguments to the callable
:var bool coalesce: whether to only run the job once when veral run times are due
:var trigger: the trigger object that controls the schedule of this job
:var str executor: the name of the executor that will run this job
芒果种子怎么种:
var int misfire_grace_time: the time (in conds) how much this job's execution is allowed to
be late
:var int max_instances: the maximum number of concurrently executing instances allowed for this
job
:var datetime.datetime next_run_time: the next scheduled run time of this job
.. note::
The ``misfire_grace_time`` has some non-obvious effects on job execution. See the
:ref:`misd-job-executions` ction in the documentation for an in-depth explanation.
"""
__slots__ = ('_scheduler', '_jobstore_alias', 'id', 'trigger', 'executor', 'func', 'func_ref',
'args', 'kwargs', 'name', 'misfire_grace_time', 'coalesce', 'max_instances',
'next_run_time')
def __init__(lf, scheduler, id=None, **kwargs):
super(Job, lf).__init__()
lf._scheduler = scheduler
lf._jobstore_alias = None
lf._modify(id=id or uuid4().hex, **kwargs)
# 间接使⽤scheduler 的⽅法:中⽌、重启、删除等。具体看scheduler 模块
def modify(lf, **changes):
"""
Makes the given changes to this job and saves it in the associated job store.
Accepted keyword arguments are the same as the variables on this class.
.
. ealso:: :meth:`~apscheduler.schedulers.dify_job`
:return Job: this job instance
"""
lf._dify_job(lf.id, lf._jobstore_alias, **changes)
return lf
def reschedule(lf, trigger, **trigger_args):
"""
Shortcut for switching the trigger on this job.
.. ealso:: :meth:`~apscheduler.schedulers.schedule_job`        :return Job: this job instance
"""
lf._schedule_job(lf.id, lf._jobstore_alias, trigger, **trigger_args)        return lf
def pau(lf):
"""
Temporarily suspend the execution of this job.
.. ealso:: :meth:`~apscheduler.schedulers.ba.BaScheduler.pau_job`
:return Job: this job instance
"""
lf._scheduler.pau_job(lf.id, lf._jobstore_alias)
return lf
def resume(lf):
"""
Resume the schedule of this job if previously paud.
.
. ealso:: :meth:`~apscheduler.schedulers.sume_job`
:return Job: this job instance
"""
lf._sume_job(lf.id, lf._jobstore_alias)
return lf
def remove(lf):
"""
Unschedules this job and removes it from its associated job store.
.. ealso:: :meth:`~apscheduler.schedulers.ve_job`
"""
lf._ve_job(lf.id, lf._jobstore_alias)
@property
def pending(lf):
"""
Returns ``True`` if the referenced job is still waiting to be added to its designated job        store.
"""
沉沦的意思
return lf._jobstore_alias is None
#
# Private API
#
def _get_run_times(lf, now):
# 利⽤trigger获得next run times
"""
Computes the scheduled run times between ``next_run_time`` and ``now`` (inclusive).        :type now: datetime.datetime从明天起做一个幸福的人
:rtype: list[datetime.datetime]
"""
run_times = []
next_run_time = lf.next_run_time
while next_run_time and next_run_time <= now:
run_times.append(next_run_time)
next_run_time = _next_fire_time(next_run_time, now)
return run_times
# 初始化具体实现,也被⽤来modify
def _modify(lf, **changes):
"""
Validates the changes to the Job and makes the modifications if and only if all of them        validate.
"""
approved = {}
样板店if 'id' in changes:
value = changes.pop('id')蠢人自己骗自己
if not isinstance(value, str):
rai TypeError("id must be a nonempty string")
if hasattr(lf, 'id'):
rai ValueError('The job ID may not be changed')
approved['id'] = value
if 'func' in changes or 'args' in changes or 'kwargs' in changes:
func = changes.pop('func') if 'func' in changes el lf.func
args = changes.pop('args') if 'args' in changes el lf.args
kwargs = changes.pop('kwargs') if 'kwargs' in changes el lf.kwargs
if isinstance(func, str):
func_ref = func
func = ref_to_obj(func)
elif callable(func):艺考报名
try:
func_ref = obj_to_ref(func)
except ValueError:
# If this happens, this Job won't be rializable
func_ref = None
el:
rai TypeError('func must be a callable or a textual reference to one')
if not hasattr(lf, 'name') ('name', None) is None:
changes['name'] = get_callable_name(func)
if isinstance(args, str) or not isinstance(args, Iterable):
rai TypeError('args must be a non-string iterable')
if isinstance(kwargs, str) or not isinstance(kwargs, Mapping):
樊锦诗事迹rai TypeError('kwargs must be a dict-like object')
check_callable_args(func, args, kwargs)
approved['func'] = func
approved['func_ref'] = func_ref
approved['args'] = args
诺基亚8800aapproved['kwargs'] = kwargs
if 'name' in changes:
value = changes.pop('name')
if not value or not isinstance(value, str):
rai TypeError("name must be a nonempty string")
approved['name'] = value
if 'misfire_grace_time' in changes:

本文发布于:2023-07-12 04:11:27,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/89/1077943.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:时间   笔记   程序   提供   芒果
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图