Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use apscheduler to realize scheduled tasks in python

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, the editor will bring you about how to use apscheduler to achieve scheduled tasks in python. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.

An easy-to-see hyperdetailed tutorial: SpringBoot Integration MybatisPlus! > > >

$pip install apscheduler

2. Source code installation (https://pypi.python.org/pypi/APScheduler/)

$python setup.py install

APScheduler has four components:

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.

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.

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.

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.

Simple application:

Import timefrom apscheduler.schedulers.blocking import BlockingScheduler def my_job (): print time.strftime ('% Y-%m-%d% HGV% MVR% slots, time.localtime (time.time ()) sched = BlockingScheduler () sched.add_job (my_job, 'interval', seconds=5) sched.start ()

The above example shows that the my_job function is executed every 5 seconds to output the current time information.

Operation operation

1. Add a job

The above is to add jobs through add_job (), and there is another way to modify the function through the scheduled_job () modifier

There is no one to answer the question? The editor has created a Python learning exchange QQ group: 857662006 look for like-minded partners to help each other, and there are also good video learning tutorials and PDF e-books in the group! Import timefrom apscheduler.schedulers.blocking import BlockingScheduler sched = BlockingScheduler () @ sched.scheduled_job ('interval', seconds=5) def my_job (): print time.strftime ('% Y-%m-%d% Hpurs% MRV% slots, time.localtime (time.time () sched.start ()

two。 Remove job

Job = scheduler.add_job (myfunc, 'interval', minutes=2) job.remove () # if there are multiple task sequences, you can set the ID number for each task, you can select the clear object according to the ID number, and the remove is not valid until it is placed in front of the start sched.add_job (myfunc,' interval', minutes=2, id='my_job_id') sched.remove_job ('my_job_id')

3. Suspend and resume jobs

Pause the job:

Apsched.job.Job.pause () apsched.schedulers.base.BaseScheduler.pause_job ()

Restore job:

Apsched.job.Job.resume () apsched.schedulers.base.BaseScheduler.resume_job ()

4. Get the job list

Get a list of scheduled jobs, which can be done using get_jobs (), which returns all job instances. Or use print_jobs () to output a list of all formatted jobs. You can also use get_job (Task ID) to get the job list for a specified task.

There is no one to answer the question? The editor has created a Python learning exchange QQ group: 857662006 look for like-minded partners to help each other, and there are also good video learning tutorials and PDF e-books in the group! Job = sched.add_job (my_job, 'interval', seconds=2, id='123') print sched.get_job (job_id='123') print sched.get_jobs ()

5. Turn off the scheduler

By default, the scheduler waits for all running jobs to complete, and then closes all schedulers and job stores. If you don't want to wait, set the wait option to False.

Sched.shutdown () sched.shutdown (wait=False)

Control of Job running (trigger)

The second parameter of add_job is trigger, which manages how jobs are scheduled. It can be date, interval or cron. For different trigger, the corresponding parameters are the same.

(1)。 Cron timing scheduling (execution at a certain time)

(int | str) indicates that the parameter can be either int type or str type (datetime | str) indicates that the parameter can be either datetime type or str type year (int | str)-4-digit year-(represents a four-digit year For example, in 2008) month (int | str)-month (1-12)-(indicates a range of values from January to December) day (int | str)-day of the (1-31)-(indicates a range of values from 1 to 31) week (int | str)-ISO week (1-53)-(31 December 2006 can be written as 2006-W52-7 (extended form) or 2006W527 (compact form) day_of_week (int | str)-number or name of weekday (0-6 or mon) Tue,wed,thu,fri,sat,sun)-(indicates the day of the week Hour (int | str)-hour (0-23)-(indicates value range 0-23:00) minute (int | str)-minute (0-59)-(indicates value range 0-59) second (int | str)-second (0-59)-(indicates value range 0-59 seconds) start_date (datetime | str)-earliest possible date/time to trigger On (inclusive)-(for start time) end_date (datetime | str)-latest possible date/time to trigger on (inclusive)-(for end time) timezone (datetime.tzinfo | str)-timezone to use for the date/time calculations (defaults to scheduler timezone)-(for time zone values) the above is how to use apscheduler to implement scheduled tasks in the python shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, 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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report