Use of apscheduler and apscheduler

Source: Internet
Author: User
Tags timedelta

Use of apscheduler and apscheduler

Recently, a program uses a background scheduled task. It looks at the python background task. Generally, there are two options: apschedery and celery. Apscheduler is more intuitive and simple. Choose this database. Searching on the internet, dizzy, a lot of write apscheduler is a super old version, and there are a lot of errors between blogs. Read the official document by yourself.

Install apscheduler in the latest version 3.0.5.

pip install apscheduler

  

Installation Complete

2.Simple task

First, let's take a simple example to see its power.

1 # coding: UTF-8 2 from apscheduler. schedulers. blocking import BlockingScheduler 3 import datetime 4 5 6 def aps_test (): 7 print datetime. datetime. now (). strftime ('% Y-% m-% d % H: % M: % s'), 'Hello, '8 9 9 10 scheduler = BlockingScheduler () 11 scheduler. add_job (func = aps_test, trigger = 'cron', second = '*/5') 12 scheduler. start ()

Check the code, define a function, define a sched type, add a job, and execute it. The code is super simple and clear. Check the results.

The function is executed in an integer multiple of 5 seconds. Is it super simple? By the way, apscheduler is easy to understand.

Write another parameter.

1 # coding: UTF-8 2 from apscheduler. schedulers. blocking import BlockingScheduler 3 import datetime 4 5 6 def aps_test (x): 7 print datetime. datetime. now (). strftime ('% Y-% m-% d % H: % M: % s'), x 8 9 scheduler = BlockingScheduler () 10 scheduler. add_job (func = aps_test, args = (' ',), trigger = 'cron', second =' */5') 11 scheduler. start ()

The result is the same as above.

Well, the above is just a small example. Let's sort it out from scratch. Apscheduler is divided into four modules:Triggers,JobStores, Executors, Schedulers. From the above example, we can see that triggers is a trigger. In the above Code, cron is used. In fact, there are other triggers to see its source code explanation.

The ``trigger`` argument can either be:          #. the alias name of the trigger (e.g. ``date``, ``interval`` or ``cron``), in which case any extra keyword             arguments to this method are passed on to the trigger's constructor          #. an instance of a trigger class

No. The Source Code explains that date, interval, and cron are available. In fact, you can understand the literal meaning. date indicates a specific one-time task, and interval indicates a cyclic task, cron indicates a scheduled task. Well, write a code to see the most obvious effect.

1 # coding: UTF-8 2 from apscheduler. schedulers. blocking import BlockingScheduler 3 import datetime 4 5 6 def aps_test (x): 7 print datetime. datetime. now (). strftime ('% Y-% m-% d % H: % M: % s'), x 8 9 scheduler = BlockingScheduler () 10 scheduler. add_job (func = aps_test, args = ('scheduled task',), trigger = 'cron', second = '*/5') 11 scheduler. add_job (func = aps_test, args = ('one-time task',), next_run_time = datetime. datetime. now () + datetime. timedelta (seconds = 12) 12 scheduler. add_job (func = aps_test, args = ('cyclic task',), trigger = 'interval', seconds = 3) 13 14 scheduler. start ()

View results

In fact, I don't need to explain the code. You can also see the results, which is very clear. Except for one-time tasks, trigger should not be written. You can define next_run_time directly. The official website does not explain the date part, but let's look at the source code and see this line of code.

 1     def _create_trigger(self, trigger, trigger_args): 2         if isinstance(trigger, BaseTrigger): 3             return trigger 4         elif trigger is None: 5             trigger = 'date' 6         elif not isinstance(trigger, six.string_types): 7             raise TypeError('Expected a trigger instance or string, got %s instead' % trigger.__class__.__name__) 8  9         # Use the scheduler's time zone if nothing else is specified10         trigger_args.setdefault('timezone', self.timezone)11 12         # Instantiate the trigger class13         return self._create_plugin_instance('trigger', trigger, trigger_args)

Row 3: If trigger is set to None, the trigger type is set to 'date. As a matter of fact, we should expand on our own to Implement Asynchronous web tasks. Assume that a task is sent to the mobile end. After the task is completed, a push task is sent to the Mobile End, which can be completed with a date trigger.

3.Logs

Well, the basic application of scheduler, I think you will, but this is just the beginning. What should I do if the code is unexpected? Will the entire task be blocked? What if I want computing-intensive tasks? The following code shows what will happen.

1 # coding: UTF-8 2 from apscheduler. schedulers. blocking import BlockingScheduler 3 import datetime 4 5 6 def aps_test (x): 7 print 1/0 8 print datetime. datetime. now (). strftime ('% Y-% m-% d % H: % M: % s'), x 9 10 scheduler = BlockingScheduler () 11 scheduler. add_job (func = aps_test, args = ('scheduled task',), trigger = 'cron', second = '*/5') 12 13 scheduler. start ()

Still the above code, but we intentionally added an error in the middle to see what would happen.

Let's say we don't have a log file. Okay, let's add a log file and see what we write.

1 # coding: UTF-8 2 from apscheduler. schedulers. blocking import BlockingScheduler 3 import datetime 4 import logging 5 6 logging. basicConfig (level = logging. INFO, 7 format = '% (asctime) s % (filename) s [line: % (lineno) d] % (levelname) s % (message) s ', 8 datefmt = '% Y-% m-% d % H: % M: % s', 9 filename='log1.txt', 10 filemode = 'A ') 11 12 13 def aps_test (x): 14 print 1/015 print datetime. datetime. now (). strftime ('% Y-% m-% d % H: % M: % s'), x16 17 scheduler = BlockingScheduler () 18 scheduler. add_job (func = aps_test, args = ('scheduled task',), trigger = 'cron', second = '*/5') 19 scheduler. _ logger = logging20 scheduler. start ()View Code

Finally, we can see the error. This is important.

In fact, most tasks can be executed here, but let's look at what else to ensure efficiency and security.

4.Delete a task

Assume that we have a wonderful task. After executing a certain stage task, we need to delete a cyclic task. Other tasks will proceed as usual. The following code is available:

1 # coding: UTF-8 2 from apscheduler. schedulers. blocking import BlockingScheduler 3 import datetime 4 import logging 5 6 logging. basicConfig (level = logging. INFO, 7 format = '% (asctime) s % (filename) s [line: % (lineno) d] % (levelname) s % (message) s ', 8 datefmt = '% Y-% m-% d % H: % M: % s', 9 filename='log1.txt', 10 filemode = 'A ') 11 12 13 def aps_test (x): 14 print datetime. datetime. now (). strftime ('% Y-% m-% d % H: % M: % s'), x15 16 17 def aps_date (x): 18 scheddate. remove_job ('interval _ task') 19 print datetime. datetime. now (). strftime ('% Y-% m-% d % H: % M: % s'), x20 21 22 scheduler = BlockingScheduler () 23 scheduler. add_job (func = aps_test, args = ('scheduled task',), trigger = 'cron', second = '*/5', id = 'cron _ task ') 24 scheduler. add_job (func = aps_date, args = ('one-time task, delete cyclic task',), next_run_time = datetime. datetime. now () + datetime. timedelta (seconds = 12), id = 'date _ task') 25 scheduler. add_job (func = aps_test, args = ('cyclic task',), trigger = 'interval', seconds = 3, id = 'interval _ task') 26 scheduler. _ logger = logging27 28 scheduler. start ()View Code

Look at the results,

When a task is successfully deleted, it defines an id for each task and remove_job. Is it super simple and intuitive? So what else?

5.Stop the task and resume the task.

Take a look at the official documents, including pause_job and resume_job, which are used in the same way as remove_job. I will not detail them here, so I will write a code.

1 # coding: UTF-8 2 from apscheduler. schedulers. blocking import BlockingScheduler 3 import datetime 4 import logging 5 6 logging. basicConfig (level = logging. INFO, 7 format = '% (asctime) s % (filename) s [line: % (lineno) d] % (levelname) s % (message) s ', 8 datefmt = '% Y-% m-% d % H: % M: % s', 9 filename='log1.txt', 10 filemode = 'A ') 11 12 13 def aps_test (x): 14 print datetime. datetime. now (). strftime ('% Y-% m-% d % H: % M: % s'), x15 16 17 def aps_pause (x): 18 scheduler. pause_job ('interval _ task') 19 print datetime. datetime. now (). strftime ('% Y-% m-% d % H: % M: % s'), x20 21 22 def aps_resume (x): 23 schedume. resume_job ('interval _ task') 24 print datetime. datetime. now (). strftime ('% Y-% m-% d % H: % M: % s'), x25 26 scheduler = BlockingScheduler () 27 scheduler. add_job (func = aps_test, args = ('scheduled task',), trigger = 'cron', second = '*/5', id = 'cron _ task ') 28 scheduler. add_job (func = aps_pause, args = ('one-time task, stop cyclic task',), next_run_time = datetime. datetime. now () + datetime. timedelta (seconds = 12), id = 'pause _ task') 29 scheduler. add_job (func = aps_resume, args = ('one-time task, Recovery Cyclic task',), next_run_time = datetime. datetime. now () + datetime. timedelta (seconds = 24), id = 'resume _ task') 30 scheduler. add_job (func = aps_test, args = ('cyclic task',), trigger = 'interval', seconds = 3, id = 'interval _ task') 31 scheduler. _ logger = logging32 33 scheduler. start ()View Code

View results

Is it easy? Now, delete the task, stop the task, and resume the task. Let's take a look at the Listening Task.

6.Unexpected

Any code may have an accident. The key is how to know it first. This is what the company cares most about. apscheduler has come up with this for us.

See the following code,

1 # coding: UTF-8 2 from apscheduler. schedulers. blocking import BlockingScheduler 3 from apscheduler. events import EVENT_JOB_EXECUTED, EVENT_JOB_ERROR 4 import datetime 5 import logging 6 7 logging. basicConfig (level = logging. INFO, 8 format = '% (asctime) s % (filename) s [line: % (lineno) d] % (levelname) s % (message) s ', 9 datefmt = '% Y-% m-% d % H: % M: % s', 10 filename='log1.txt', 11 filemode = 'A') 12 13 14 def aps_test (x ): 15 print datetime. datetime. now (). strftime ('% Y-% m-% d % H: % M: % s'), x16 17 18 def date_test (x): 19 print datetime. datetime. now (). strftime ('% Y-% m-% d % H: % M: % s'), x20 print 1/021 22 23 def my_listener (event): 24 if event. exception: 25 print 'Task Error !!!!!! '26 else: 27 print 'the task runs as usual... '28 29 scheduler = BlockingScheduler () 30 scheduler. add_job (func = date_test, args = ('fixed task, error occurred ',), next_run_time = datetime. datetime. now () + datetime. timedelta (seconds = 15), id = 'date _ task') 31 scheduler. add_job (func = aps_test, args = ('cyclic task',), trigger = 'interval', seconds = 3, id = 'interval _ task') 32 scheduler. add_listener (my_listener, EVENT_JOB_EXECUTED | EVENT_JOB_ERROR) 33 schedted. _ logger = logging34 35 scheduler. start ()View Code

View results

Is it intuitive? In the production environment, you can replace the error message with an email or a text message, so that the scheduled task can immediately know when an error occurs.

Now, let's talk about this. We will have the opportunity to expand this apscheduler, a very powerful and intuitive background task library.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.