我使用Python apscheduler(版本3.0.1)每秒执行一个函数
码:
scheduler = BackgroundScheduler()
scheduler.add_job(runsync, 'interval', seconds=1)
scheduler.start()
它大部分时间都很好,但有时我会收到这个警告:
WARNING:apscheduler.scheduler:Execution of job "runsync (trigger: interval[0:00:01], next run at: 2015-12-01 11:50:42 UTC)" skipped: maximum number of running instances reached (1)
1.这是执行此方法的正确方法吗?
2.这个警告意味着什么?它是否会影响函数内部任务的执行?
3.如何处理?
答案 0 :(得分:9)
这意味着任务花费的时间超过一秒,默认情况下,给定作业只允许一次并发执行。我不知道如何在不知道任务是什么的情况下处理这个问题。
答案 1 :(得分:2)
如果要同时运行同一作业的实例并避免出现警告,可以在调度程序的max_instances
方法中包含add_job()
参数。默认值为1。
答案 2 :(得分:1)
如果特定用例允许,只需增加max_instances
,如下所示。
import apscheduler.schedulers.blocking
scheduler = apscheduler.schedulers.blocking.BackgroundScheduler('apscheduler.job_defaults.max_instances': '2')
有三种apscheduler
配置样式。这些在documentation中描述。
答案 3 :(得分:0)
答案 4 :(得分:0)
为了顺利执行,您还可以尝试 simple-scheduler。为什么?
事件调度程序示例:
from time import sleep, ctime, time
from simple_scheduler.event import event_scheduler
event_scheduler.timezones()
TZ = "Asia/Kolkata"
WHEN = ["mon|09:**", "*|10:45"] #[mon/tue/wed/thu/fri/sat/sun] or "*" for all days
# correct argument precedence in a function
def print_args(a, b=1, *args, **kwargs):
print(ctime(time()), a, b, args, kwargs)
# the above print_args function would be called twice,
# so to differentiate between then use "job_name"
event_scheduler.add_job(target = print_args,
args = (0,), # don't forget "," for single arguments
kwargs = {"b":2},
when = WHEN,
tz = TZ,
job_name = "print-args-1")
event_scheduler.add_job(target= print_args,
args = (0, 2, "arg1", "arg2"),
kwargs = {"key1":"value1",
"key2":"value2"},
when = WHEN,
tz = TZ,
job_name = "print-args-2")
event_scheduler.verbose = True # (default)
event_scheduler.job_summary()
event_scheduler.run()
event_scheduler.job_summary()
sleep(200)
event_scheduler.remove_job("print-args-2")
sleep(5)
event_scheduler.clear()
循环调度器示例:
from time import sleep, ctime, time
from simple_scheduler.recurring import recurring_scheduler
def wait_10_secs(t):
wait_t_secs(10)
def wait_t_secs(t):
began_at = ctime(time())
sleep(t)
print(f"I waited {t} seconds. [From: {began_at} to {ctime(time())}]")
# the above wait_t_secs function would be called twice,
# so to differentiate between then use "job_name"
recurring_scheduler.add_job(target=wait_10_secs,
period_in_seconds=5, # period < execution time
job_name="ten",
kwargs={"t":10})
recurring_scheduler.add_job(target=wait_t_secs,
kwargs={"t":7}, # period < execution time
period_in_seconds=5,
job_name="seven")
recurring_scheduler.add_job(target=wait_t_secs,
kwargs={"t":3}, # period > execution time
period_in_seconds=5,
job_name="three")
recurring_scheduler.verbose = True # (default)
recurring_scheduler.job_summary()
recurring_scheduler.run()
recurring_scheduler.job_summary()
sleep(10)
print("Removing job with name 'ten'...")
recurring_scheduler.remove_job("ten")
sleep(10)
print("Removing job with name 'ten'...")
recurring_scheduler.remove_job("ten")
sleep(10)
print("Stopping and clearing the scheduler.\n")
recurring_scheduler.clear()