Timing task module APScheduler python

A simple task

Definition of a function, and a scheduler defined type, adding a Job, and then executed, it

Integer multiples of 5 seconds, to perform this function

# coding:utf-8
from apscheduler.schedulers.blocking import BlockingScheduler
import datetime


def aps_test():
    print (datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), '你好')


scheduler = BlockingScheduler()
scheduler.add_job(func=aps_test, trigger='cron', second='*/5')
scheduler.start()

With arguments

# coding:utf-8
from apscheduler.schedulers.blocking import BlockingScheduler
import datetime


def aps_test(x):
    print (datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), x)

scheduler = BlockingScheduler()
scheduler.add_job(func=aps_test, args=('你好',), trigger='cron', second='*/5')
scheduler.start()
# coding:utf-8
from apscheduler.schedulers.blocking import BlockingScheduler
import datetime


def aps_test(x):
    print (datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), x)

scheduler = BlockingScheduler()
scheduler.add_job(func=aps_test, args=('定时任务',), trigger='cron', second='*/5')
scheduler.add_job(func=aps_test, args=('一次性任务',), next_run_time=datetime.datetime.now() + datetime.timedelta(seconds=12))
scheduler.add_job(func=aps_test, args=('循环任务',), trigger='interval', seconds=3)

scheduler.start()

Second, log

# coding:utf-8
from apscheduler.schedulers.blocking import BlockingScheduler
import datetime
import logging

logging.basicConfig(level=logging.INFO,
                    format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
                    datefmt='%Y-%m-%d %H:%M:%S',
                    filename='log1.txt',
                    filemode='a')


def aps_test(x):
    print 1/0
    print (datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), x)

scheduler = BlockingScheduler()
scheduler.add_job(func=aps_test, args=('定时任务',), trigger='cron', second='*/5')
scheduler._logger = logging
scheduler.start()

Third, delete the task

After a certain stage required to perform the task, delete a cyclical task, other tasks as usual. We have the following code:

# coding:utf-8
from apscheduler.schedulers.blocking import BlockingScheduler
import datetime
import logging

logging.basicConfig(level=logging.INFO,
                    format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
                    datefmt='%Y-%m-%d %H:%M:%S',
                    filename='log1.txt',
                    filemode='a')


def aps_test(x):
    print (datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), x)


def aps_date(x):
    scheduler.remove_job('interval_task')
    print (datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), x)
    

scheduler = BlockingScheduler()
scheduler.add_job(func=aps_test, args=('定时任务',), trigger='cron', second='*/5', id='cron_task' ) 
Scheduler.add_job (FUNC = aps_date, args = ( ' time task, remove cyclic tasks ' ,), next_run_time datetime.datetime.now = () + the datetime.timedelta (seconds The 12 is =), ID = ' date_task ' ) 
scheduler.add_job (FUNC = aps_test, args = ( ' cycle task ' ,), Trigger = ' interval The ' , = seconds The. 3, ID = ' interval_task ' ) 
scheduler._logger = the logging 

scheduler.start ()

Fourth, the task is stopped, recovery task

# coding:utf-8
from apscheduler.schedulers.blocking import BlockingScheduler
import datetime
import logging

logging.basicConfig(level=logging.INFO,
                    format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
                    datefmt='%Y-%m-%d %H:%M:%S',
                    filename='log1.txt',
                    filemode='a')


def aps_test(x):
    print (datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), x)


def aps_pause(x):
    scheduler.pause_job('interval_task')
    print (datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), x)


def aps_resume(x):
    scheduler.resume_job('interval_task')
    print (datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), x)

scheduler = BlockingScheduler()
scheduler.add_job (FUNCaps_test =, args = ( ' scheduled task ' ,), Trigger = ' the cron ' , SECOND = ' * /. 5 ' , ID = ' cron_task ' ) 
scheduler.add_job (FUNC = aps_pause, args = ( ' time task, stop cyclic task ' ,), next_run_time datetime.datetime.now = () + the datetime.timedelta (seconds The 12 is =), ID = ' pause_task ' ) 
scheduler.add_job (FUNC = aps_resume, args = ( ' time task, task recovery cycle ' ,), next_run_time datetime.datetime.now = () + the datetime.timedelta (= 24 seconds The), ID = ' resume_task ')
scheduler.add_job(func=aps_test, args=('循环任务',), trigger='interval', seconds=3, id='interval_task')
scheduler._logger = logging

scheduler.start()

V. capture error

# coding:utf-8
from apscheduler.schedulers.blocking import BlockingScheduler
from apscheduler.events import EVENT_JOB_EXECUTED, EVENT_JOB_ERROR
import datetime
import logging

logging.basicConfig(level=logging.INFO,
                    format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
                    datefmt='%Y-%m-%d %H:%M:%S',
                    filename='log1.txt',
                    the fileMode = ' A ' ) 


DEF aps_test (X):
     Print (datetime.datetime.now () the strftime. ( ' % Y-M-% D%% H:% M:% S ' ), X) 


DEF date_test (X ):
     Print (datetime.datetime.now () the strftime. ( ' % Y-M-% D%% H:% M:% S ' ), X)
     Print (. 1/ 0) 


DEF my_listener (Event):
     IF Event .exception:
         Print ( ' task wrong !!!!!! ') 
    the else :
         Print ( ' task to run as usual ...')

scheduler = BlockingScheduler()
scheduler.add_job(func=date_test, args=('一定性任务,会出错',), next_run_time=datetime.datetime.now() + datetime.timedelta(seconds=15), id='date_task')
scheduler.add_job(func=aps_test, args=('循环任务',), trigger='interval', seconds=3, id='interval_task')
scheduler.add_listener(my_listener, EVENT_JOB_EXECUTED | EVENT_JOB_ERROR)
scheduler._logger = logging

scheduler.start()

 

Guess you like

Origin www.cnblogs.com/angelyan/p/11297252.html