In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the python timing task apscheduler how to use the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that everyone after reading this python timing task apscheduler how to use the article will have a harvest, let's take a look.
Install the main components of pip install apscheduler
Conceptual things, the face is familiar, the code is easier to understand than these definitions.
Triggers (trigger) contain scheduling logic, and each job has its own trigger to determine which job will run next. Except for their own initial configuration accidents, triggers are completely stateless. Human speaking is the way you specify to trigger the current task.
Type interpretation DateTrigger expiration execution (to xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Job storage (job store) stores scheduled jobs, the default job storage is simply to save the job in memory, other job storage is to save the job in the database. The data of a job is serialized when it is saved in the persistent job store and deserialized when loaded. The scheduler cannot share the same job store.
Jobstore is initialized in scheduler, and Jobstore can also be dynamically added through scheduler's add_jobstore. Every jobstore
Will bind an alias,scheduler when Add Job, find the corresponding jobstore in the scheduler according to the specified jobstore, and
Add job to jobstore.
Jobstore is mainly implemented through loads and dumps of pickle library. [the core is rewritten through _ _ getstate__ and _ _ setstate__ of python.
Implement], save Job dynamically to storage every time you change, and then load it dynamically when you use it, either redis or redis
It is a database [integrating multiple databases through the sqlarchemy library], or it can be mongodb, etc.
Currently, Jobstore supported by APScheduler:
MemoryJobStore
MongoDBJobStore
RedisJobStore
RethinkDBJobStore
SQLAlchemyJobStore
ZooKeeperJobStore
Executors (executor) handle the running of jobs, usually by submitting callable objects defined in the job to a thread or to a city pool. When the job is completed, the executor will notify the scheduler.
Human talk is packaged with it when adding tasks. The type of executor will be selected according to different scheduling. If you choose AsyncIO as the scheduling library, then select AsyncIOExecutor, if you choose tornado as the scheduling library, select TornadoExecutor, if you choose to start the process as the scheduling, you can choose ThreadPoolExecutor or ProcessPoolExecutor. You can choose different executors according to the actual scheduler.
Currently, Executor supported by APScheduler:
AsyncIOExecutor
GeventExecutor
ThreadPoolExecutor
ProcessPoolExecutor
TornadoExecutor
TwistedExecutor
The scheduler (scheduler) is another component. You usually have only one scheduler in the application, and the application developer usually does not directly deal with job stores, schedulers, and triggers. Instead, the scheduler provides the appropriate interface to handle these. Configuring job stores and executors can be done in the scheduler, such as adding, modifying, and removing jobs.
Scheduler is the core of APScheduler, through which all related components are defined. After scheduler starts, it starts to be scheduled according to the configured tasks.
Except for the wake-up schedule that will be scheduled based on all the trigger that defines the Job. Scheduling is also triggered when Job information changes.
Scheduler can choose different components according to its own needs. If you use AsyncIO, you can choose AsyncIOScheduler, and if you use tornado, you can choose different components.
Select TornadoScheduler.
Currently, Scheduler supported by APScheduler:
AsyncIOScheduler
BackgroundScheduler
BlockingScheduler
GeventScheduler
QtScheduler
TornadoScheduler
TwistedScheduler
Simply apply import timefrom apscheduler.schedulers.blocking import BlockingScheduler # to introduce background def my_job (): print time.strftime ('% Y-%m-%d% HV% MV% time.localtime, time.localtime (time.time ()) sched = BlockingScheduler () sched.add_job (my_job, 'interval', seconds=5) sched.start () complete Code # trigeers trigger # job stores job Storage # executors Actuator # schedulers Scheduler from pytz import utcfrom sqlalchemy import funcfrom apscheduler.schedulers.background import BackgroundScheduler AsyncIOSchedulerfrom apscheduler.jobstores.sqlalchemy import SQLAlchemyJobStorefrom apscheduler.executors.pool import ProcessPoolExecutorjobstores = {# multiple storage can be configured # 'mongo': {' type': 'mongodb'},' default': SQLAlchemyJobStore (url='sqlite:///jobs.sqlite') # SQLAlchemyJobStore specify storage link} executors = {'default': {' type': 'threadpool',' max_workers': 20} # maximum number of worker threads 20 'processpool': ProcessPoolExecutor (max_workers=5) # maximum number of worker processes is 5} job_defaults = {' coalesce': False, # turn off merging of new job When job is delayed or abnormal reasons are not executed, 'max_instances': 3 # runs concurrently the default maximum instance of the new job} scheduler = BackgroundScheduler () #. Do something else here, maybe add jobs etc.scheduler.configure (jobstores=jobstores, executors=executors, job_defaults=job_defaults, timezone=utc) # utc time zone import osimport timedef print_time (name) as scheduler: print (f'{name}-{time.ctime ()}') def add_job (job_id, func, args, seconds): "" add job "" print (f "add interval execution task job-{job_id}") scheduler.add_job (id=job_id Func=func, args=args, trigger='interval', seconds=seconds) def add_coun_job (job_id, func, args, start_time): "" add job "" print (f "add one-time execution task job-{job_id}") scheduler.add_job (id=job_id, func=func, args=args, trigger='date',timezone='Asia/Shanghai', run_date=start_time) # scheduler.add_job (func=print_time, trigger='date',timezone='Asia/Shanghai', run_date=datetime (2022) 2, 19, 17, 57, 0). Astimezone () Args= ['text2']) def remove_job (job_id): "remove job"scheduler.remove_job (job_id) print (f" remove job-{job_id} ") def pause_job (job_id):" stop job "" scheduler.pause_job (job_id) print (f "stop job-{job_id}") def resume_job (job_id): "" restore job "" scheduler.resume_job (job_id) print (f "restore job-{job_id}") def get_jobs (): "get all job information Including stopped "" res = scheduler.get_jobs () print (f "all job-{res}") def print_jobs (): print (f "detailed job information") scheduler.print_jobs () def start (): "start the scheduler" scheduler.start () def shutdown (): "close the scheduler"scheduler.shutdown () if _ _ name_" _ = ='_ _ main__': scheduler = BackgroundScheduler () # start () # print ('Press Ctrl+ {0} to exit\ n'.format (' Break' if os.name = = 'nt' else' C')) # add_job ('job_A' Func=print_time, args= ("A",), seconds=1) # add_job ('job_B', func=print_time, args= ("B",) Seconds=2) # time.sleep (6) # pause_job ('job_A') # stop a # get_jobs () # get all job # time.sleep (6) # print_jobs () # resume_job (' job_A') # time.sleep (6) # remove_job ('job_A') # time.sleep (6) from datetime import datetime import pytz start () date_temp = datetime (2022) 2, 19, 17, 30, 5) # scheduler.add_job (print_time, 'date', run_date=datetime.now (pytz.timezone (' America/Manaus')), args= ['text']) # scheduler.add_job (print_time,' date',timezone='Asia/Shanghai', run_date=datetime (2022, 2, 19, 17, 57, 0). Astimezone (), args= ['text2']) add_coun_job (job_id= "job_C", func=print_time Args= ('one-time execution of tasks',), start_time=datetime (2022, 2, 19, 18, 4, 0). Astimezone () time.sleep (130) try: shutdown () except RuntimeError: pass on "how to use python scheduled tasks apscheduler" ends here Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to use python timed tasks apscheduler". If you want to learn more, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.