本文整理汇总了Python中apscheduler.scheduler.Scheduler.add_job方法的典型用法代码示例。如果您正苦于以下问题:Python Scheduler.add_job方法的具体用法?Python Scheduler.add_job怎么用?Python Scheduler.add_job使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类apscheduler.scheduler.Scheduler
的用法示例。
在下文中一共展示了Scheduler.add_job方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: wrap
# 需要导入模块: from apscheduler.scheduler import Scheduler [as 别名]
# 或者: from apscheduler.scheduler.Scheduler import add_job [as 别名]
def wrap(**options):
sched = Scheduler()
sched.start()
seconds = 5
interval = timedelta(seconds = seconds)
#trigger = RandomizedIntervalTrigger(interval, randomize = lambda : uniform(seconds / 8, seconds / 4))
trigger = RandomizedIntervalTrigger(interval)
sched.add_job(trigger, func, None, None, **{} )
示例2: PyFlowScheduler
# 需要导入模块: from apscheduler.scheduler import Scheduler [as 别名]
# 或者: from apscheduler.scheduler.Scheduler import add_job [as 别名]
#.........这里部分代码省略.........
if not doit: return
prefix = os.path.basename(self.flow.workdir) + "_"
import tempfile, datetime
suffix = str(datetime.datetime.now()).replace(" ", "-")
# Remove milliseconds
i = suffix.index(".")
if i != -1: suffix = suffix[:i]
suffix += ".tar.gz"
#back = os.getcwd()
#os.chdir(self.customer_service_dir.path)
_, tmpname = tempfile.mkstemp(suffix="_" + suffix, prefix=prefix,
dir=self.customer_service_dir.path, text=False)
print("Dear customer,\n We are about to generate a tarball in\n %s" % tmpname)
self.flow.make_light_tarfile(name=tmpname)
#os.chdir(back)
def start(self):
"""
Starts the scheduler in a new thread. Returns 0 if success.
In standalone mode, this method will block until there are no more scheduled jobs.
"""
self.history.append("Started on %s" % time.asctime())
self.start_time = time.time()
if not has_apscheduler:
raise RuntimeError("Install apscheduler with pip")
if has_sched_v3:
self.sched.add_job(self.callback, "interval", **self.sched_options)
else:
self.sched.add_interval_job(self.callback, **self.sched_options)
errors = self.flow.look_before_you_leap()
if errors:
self.exceptions.append(errors)
return 1
# Try to run the job immediately. If something goes wrong return without initializing the scheduler.
self._runem_all()
if self.exceptions:
self.cleanup()
self.send_email(msg="Error while trying to run the flow for the first time!\n %s" % self.exceptions)
return 1
try:
self.sched.start()
return 0
except KeyboardInterrupt:
self.shutdown(msg="KeyboardInterrupt from user")
if ask_yesno("Do you want to cancel all the jobs in the queue? [Y/n]"):
print("Number of jobs cancelled:", self.flow.cancel())
self.flow.pickle_dump()
return -1
def _runem_all(self):
"""
This function checks the status of all tasks,
tries to fix tasks that went unconverged, abicritical, or queuecritical
示例3: PyFlowScheduler
# 需要导入模块: from apscheduler.scheduler import Scheduler [as 别名]
# 或者: from apscheduler.scheduler.Scheduler import add_job [as 别名]
#.........这里部分代码省略.........
flow.show_status()
raise self.Error("""\
pid_file %s already exists
There are two possibilities:
1) There's an another instance of PyFlowScheduler running
2) The previous scheduler didn't exit in a clean way
To solve case 1:
Kill the previous scheduler (use 'kill pid' where pid is the number reported in the file)
Then you can restart the new scheduler.
To solve case 2:
Remove the pid_file and restart the scheduler.
Exiting""" % pid_file)
with open(pid_file, "w") as fh:
fh.write(str(self.pid))
self._pid_file = pid_file
self._flow = flow
def start(self):
"""
Starts the scheduler in a new thread. Returns True if success.
In standalone mode, this method will block until there are no more scheduled jobs.
"""
self.history.append("Started on %s" % time.asctime())
self.start_time = time.time()
if has_sched_v3:
self.sched.add_job(self.callback, "interval", **self.sched_options)
else:
self.sched.add_interval_job(self.callback, **self.sched_options)
errors = self.flow.look_before_you_leap()
if errors:
self.exceptions.append(errors)
return False
# Try to run the job immediately. If something goes wrong return without initializing the scheduler.
self._runem_all()
if self.exceptions:
self.cleanup()
self.send_email(msg="Error while trying to run the flow for the first time!\n %s" % self.exceptions)
return False
try:
self.sched.start()
return True
except KeyboardInterrupt:
self.shutdown(msg="KeyboardInterrupt from user")
if ask_yesno("Do you want to cancel all the jobs in the queue? [Y/n]"):
self.flow.cancel()
self.flow.pickle_dump()
return False
def _runem_all(self):
"""
This function checks the status of all tasks,
tries to fix tasks that went unconverged, abicritical, or queuecritical
and tries to run all the tasks that can be submitted.+
示例4: Scheduler
# 需要导入模块: from apscheduler.scheduler import Scheduler [as 别名]
# 或者: from apscheduler.scheduler.Scheduler import add_job [as 别名]
import datetime
import time
import os
from apscheduler.scheduler import Scheduler
# Start the scheduler
sched = Scheduler()
sched.daemonic = False
sched.start()
def job_function():
os.system(" spark-submit --class ClimateClustering --master local[4] CSYE7374_Final_Spark-assembly-1.0.jar ec2-52-20-252-81.compute-1.amazonaws.com")
os.system(" spark-submit --class ClimateARIMA --master local[4] CSYE7374_Final_Spark-assembly-1.0.jar ec2-52-20-252-81.compute-1.amazonaws.com")
print(datetime.datetime.now())
time.sleep(20)
# Schedules job_function to be run once each minute
sched.add_job(job_function, "cron", hour='0', minute='0',second='0')
示例5: sun
# 需要导入模块: from apscheduler.scheduler import Scheduler [as 别名]
# 或者: from apscheduler.scheduler.Scheduler import add_job [as 别名]
else:
# send sun (default) and send message for me
bot.send_message(shut_down_alert, "description to add: {0}".format(main_current))
bot.send_message(dest, emoji.emojize("{0} - {1} \nTemperatura maxima: {2} C\nTemperatura minima: {3} C\nUmidade de {4}%\n\n==============\nCreated by @JGabrielFreitas\nPowered by OpenWeather API".format(emoji_to_show, description, temp_max, temp_min, humidity))) # dest and msg
# print emoji.emojize("{0} - {1} \nTemperatura maxima: {2} C\nTemperatura minima: {3} C\nUmidade de {4}%\n\n==============\nCreated by @JGabrielFreitas\nPowered by OpenWeather API".format(emoji_to_show, description, temp_max, temp_min, humidity))
except:
bot.send_message(shut_down_alert, "Dude, your bot is down...:\n\n{0}".format(traceback.format_exc()))
# @sched.interval_schedule()
def timed_job():
print('This job is run every one minutes.')
job()
# @sched.scheduled_job('cron', day_of_week='mon-fri', hour=17)
# def scheduled_job():
# print('This job is run every weekday at 5pm.')
# job()
# run jobs
# schedule.every().hour.do(job)
# schedule.every(1).minutes.do(job)
#
# while True:
# schedule.run_pending()
# time.sleep(1)
# job()
sched.add_job(timed_job, 'interval', seconds=10, id="timed_job")
sched.start()
示例6: SchedulerContainer
# 需要导入模块: from apscheduler.scheduler import Scheduler [as 别名]
# 或者: from apscheduler.scheduler.Scheduler import add_job [as 别名]
class SchedulerContainer( DaemonContainer ):
def __init__(self, environment):
super(Scheduler, self).__init__(environment)
gconfig = environment.get("gconfig", {})
options = environment.get("options", {})
self.scheduler = Scheduler(gconfig, **options)
def on_start(self):
self.scheduler.start()
def on_stop(self):
self.scheduler.stop()
def unschedule_func(self, func):
self.scheduler.unschedule_func(func)
def unschedule_job(self, job):
self.scheduler.unschedule_job(job)
def add_interval_job(self, func, weeks=0, days=0, hours=0, minutes=0, seconds=0, start_date=None, args=None, kwargs=None, **options):
return self.scheduler.add_interval_job(func=func,
weeks=weeks,
days=days,
hours=hours,
minutes=minutes,
seconds=seconds,
start_date=start_date,
args=args,
kwargs=kwargs,
**options)
def add_cron_job(self, func, year=None, month=None, day=None, week=None, day_of_week=None, hour=None, minute=None, second=None, start_date=None, args=None, kwargs=None, **options):
return self.scheduler.add_cron_job(func=func,
year=year,
month=month,
day=day,
week=week,
day_of_week=day_of_week,
hour=hour,
minute=minute,
second=second,
start_date=start_date,
args=args,
kwargs=kwargs,
**options)
def add_date_job(self, func, date, args=None, kwargs=None, **options):
return self.scheduler.add_date_job(func=func,
date=date,
args=args,
kwargs=kwargs,
**options)
def get_jobs(self):
return self.scheduler.get_jobs()
def add_job(self, trigger, func, args, kwargs, jobstore='default', **options):
return self.scheduler.add_job(trigger=trigger,
func=func,
args=args,
kwargs=kwargs,
jobstore=jobstore,
**options)
def add_listener(self, callback, mask):
self.scheduler.add_listener(callback, mask)
def remove_listener(self, callback):
self.scheduler.remove_listener(callback)
示例7: FreshList
# 需要导入模块: from apscheduler.scheduler import Scheduler [as 别名]
# 或者: from apscheduler.scheduler.Scheduler import add_job [as 别名]
class FreshList(object):
''' A dictionary of fresh StateObjects maintained in soft state fashion. (*NOTE: perhaps should rename it to FreshDict or something less misleading*).
Timedout StateOjbect will be reaped, and a reap callback will to applied on them before reaping.
A function that refresh the local user will be called periodically (*This SHOULD be refactored out of this class*).
This list is protected by a recurisve lock for operation (both read and write).
'''
_logger = Logger.get_logger('FreshList')
reap_interval = StateObject.default_ttl * 2
def __init__(self, refresh_func, reap_callback, *args, **kwargs):
'''
Args:
refresh_func: Callback function to refresh local user.
reap_callback: Callback function to be applied to the StateObject to be reaped.
'''
super(FreshList, self).__init__()
self.instances = dict()
self.refresh_func = refresh_func
self.reap_callback = reap_callback
# every operation to self.instances should grab self.__rlock first
self.__rlock = RLock()
self.scheduler = Scheduler()
self.scheduler.start()
# schedule periodic reap
reap_interval = FreshList.reap_interval
self.scheduler.add_job(RandomizedIntervalTrigger(timedelta(seconds = reap_interval), randomize = lambda: uniform(0, reap_interval / 4)), self.reap, None, None, **{})
# schedule refresh self
refresh_interval = 0.7 * StateObject.default_ttl
self.scheduler.add_job(RandomizedIntervalTrigger(timedelta(seconds = refresh_interval), randomize = lambda: uniform(0, refresh_interval / 4)), self.refresh_func, None, None, **{})
def schedule_next(self, interval, func):
'''A convenience function to schedule a task.
Args:
interval (float): The time from now to execute the task in seconds.
func: The task to be executed.
'''
self.scheduler.add_date_job(func, datetime.now() + timedelta(seconds = interval))
def shutdown(self, wait = False):
'''Shutdown the scheduler of this object.
'''
self.scheduler.shutdown(wait = False)
def reap(self):
'''Checks the StateObjects and reap the ones that are timed out.
'''
with self.__rlock:
zombies = filter(lambda(k, state_object): not state_object.is_active(), self.instances.iteritems())
self.instances = dict(filter(lambda (k, state_object): state_object.is_active(), self.instances.iteritems()))
map(lambda(k, state_object): self.reap_callback(state_object), zombies)
def announce_received(self, k):
'''Refresh stateobject when a refreshing announcement has been received for the object
Args:
k : the key for the state object to be refreshed
'''
with self.__rlock:
self.instances[k].refresh_timestamp()
def __getitem__(self, k):
with self.__rlock:
return self.instances.get(k, None)
def __setitem__(self, k, state_object):
with self.__rlock:
self.instances[k] = state_object
def __delitem__(self, k):
with self.__rlock:
try:
del self.instances[k]
except KeyError as e:
FreshList._logger.exception("Try to del non-exist state object")
raise e
def __len__(self):
with self.__rlock:
return len(self.instances)
def clear(self):
'''Clear the dict.
'''
with self.__rlock:
self.instances.clear()
def has_key(self, key):
'''Check whether the key exists in the current dict
'''
with self.__rlock:
return self.instances.has_key(key)
def keys(self):
'''Get all keys.
'''
with self.__rlock:
#.........这里部分代码省略.........