程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python timing task implementation

編輯:Python

python Achieve timed tasks

loop sleep The way

  • This is the easiest way to achieve , Put the task to be executed in the loop , then sleep For a period of time . The disadvantage of this method is that it can only be executed at fixed time intervals , If there is a scheduled task, it cannot be completed .,n The value needs to be calculated by yourself , also sleep Will block the execution of the process , During the blocking process, the program can only wait for nothing .

    import time
    from datetime import datetime
    # Every time n Once per second 
    def timer(n):
    while True:
    print('do something...')
    time.sleep(n)
    

threading Modular Timer

  • threding Module Timer Is a non blocking function , The program will not block , But it still can't be executed regularly .

Timer Use

from threading import Timer

Time(ince, printTime, (time, ))

Timer The first argument to the function is the time interval ( The unit is seconds ), The second parameter is the name of the function to be called , The third function is the parameter of the calling function (tuple)

from threading import Timer
from datetime import datetime
def printTime(time):
print(datetime.now().strftime('%Y-%m-%d'))
time = 3
t = Timer(time, printTime, (time, ))
t.start()
print('over')
Output :
>>> over
>>> 2020-12-31

Use sched modular

  • sched The module is python Built in modules , It is a scheduling ( Delay processing mechanism ), Every time you want to execute a task regularly, you must write a schedule . And the program will be blocked and still cannot be executed

  • sched usage

    • Generate scheduler

      schedule = sched.scheduler(time.time, time.sleep) The first argument is a function that returns a timestamp , The second parameter can block before the timing arrives ( You don't have to pass it , Default time.sleep)

    • Add scheduling event

      Scheduling events :enter、enterables etc. ,s.enter(param1, param2, param3, param4), The four parameters are respectively : The time interval 、 priority ( It is used for the execution sequence of two events that arrive at the same time )、 The function triggered by the call 、 The function triggered by the call ( Be careful : Be sure to tuple Pass in , If there are no parameters ())

    • function

      s.run(), Be careful :sched The module does not execute circularly , Once the scheduling is completed, it is over , If you want to execute again enter

    import sched
    import time
    from datetime import datetime
    # initial sched modular schedule class 
    schedule = sched.scheduler(time.time, time.sleep)
    # Called function 
    def printTime():
    print(datetime.now().strftime(r'%Y-%m-%d'))
    def main(interval):
    # enter The four parameters are respectively : Interval Events 、 priority ( For simultaneous execution of two events arriving at the same time )、 The function triggered by the call ,
    schedule.enter(interval, 0, printTime, ())
    schedule.run()
    main(5)
    print('overs')
    Output :
    >>> 2020-12-31
    >>> over
    

APScheduler Timed frame

APScheduler It's a python Timing task framework , It's very convenient to use . Provide date based 、 Fixed intervals and crontab Type of task , And it can persist tasks 、 And daemon Mode to run the application .

APScheduler Use

  • pip install APScheduler

  • APScheduler Using examples

    from apscheduler.schedulers.blocking import BlockingScheduler
    from datetime import datetime
    def printTime():
    print(datetime.now().strftime(r'%Y-%m-%d'))
    scheduler = BlockingScheduler()
    scheduler.add_job(printTime, 'interval', seconds=2)
    scheduler.start()
    
  • APScheduler Four components

    APScheduler The four components are : trigger (ttigger)、 Job storage (job store)、 actuator (e xecutor)、 Scheduler (scheduler)

    • trigger (tigger)

      Including scheduling logic, each job has its tenant trigger , Used to decide which job will run next , Triggers are completely stateless except for their initial configuration .

      APScheduler There are three built-in tigger( trigger )

      data: Trigger at a specific point in time

      interval: Trigger at regular intervals

      cron: Trigger periodically at a specific time

      date

      The most basic kind of scheduling , The job is executed only once . Its parameters are as follows :

      • run_date (datetime|str) – the date/time to run the job at
      • timezone (datetime.tzinfo|str) – time zone for run_date if it doesn’t have one already
      from datetime import date
      from apscheduler.schedulers.blocking import BlockingScheduler
      sched = BlockingScheduler()
      def my_job(text):
      print(text)
      # The job will be executed on November 6th, 2009
      sched.add_job(my_job, 'date', run_date=date(2009, 11, 6), args=['text'])
      sched.add_job(my_job, 'date', run_date=datetime(2009, 11, 6, 16, 30, 5), args=['text'])
      sched.add_job(my_job, 'date', run_date='2009-11-06 16:30:05', args=['text'])
      # The 'date' trigger and datetime.now() as run_date are implicit
      sched.add_job(my_job, args=['text'])
      sched.start()
      

      cron

      • year (int|str) – 4-digit year
      • month (int|str) – month (1-12)
      • day (int|str) – day of the (1-31)
      • week (int|str) – ISO week (1-53)
      • day_of_week (int|str) – number or name of weekday (0-6 or mon,tue,wed,thu,fri,sat,sun)
      • hour (int|str) – hour (0-23)
      • minute (int|str) – minute (0-59)
      • second (int|str) – second (0-59)
      • start_date (datetime|str) – earliest possible date/time to trigger on (inclusive)
      • end_date (datetime|str) – latest possible date/time to trigger on (inclusive)
      • timezone (datetime.tzinfo|str) – time zone to use for the date/time calculations (defaults to scheduler timezone)

      expression :

      from apscheduler.schedulers.blocking import BlockingScheduler
      def job_function():
      print("Hello World")
      # BlockingScheduler
      sched = BlockingScheduler()
      # Schedules job_function to be run on the third Friday
      # of June, July, August, November and December at 00:00, 01:00, 02:00 and 03:00
      sched.add_job(job_function, 'cron', month='6-8,11-12', day='3rd fri', hour='0-3')
      # Runs from Monday to Friday at 5:30 (am) until 2014-05-30 00:00:00
      sched.add_job(job_function, 'cron', day_of_week='mon-fri', hour=5, minute=30, end_date='2014-05-30')
      sched.start()
      

      interval

      Parameters :

      • weeks (int) – number of weeks to wait
      • days (int) – number of days to wait
      • hours (int) – number of hours to wait
      • minutes (int) – number of minutes to wait
      • seconds (int) – number of seconds to wait
      • start_date (datetime|str) – starting point for the interval calculation
      • end_date (datetime|str) – latest possible date/time to trigger on
      • timezone (datetime.tzinfo|str) – time zone to use for the date/time calculations
    • Job storage (job store)

      Store scheduled jobs , The default job store is simply to save jobs in memory , Other job stores store jobs in databases , The data of a job will be serialized when it is stored in the persistent job store , And is deserialized at load time , And the scheduler cannot share the same job store .

      APScheduler By default MemoryJobStore

    • actuator

      Process the run of the job , They usually submit the specified callable objects to a thread or pool in the job , When the job is completed, the executor will notify the scheduler .

      There are two commonly used executive organs :

      ProcessPoolExecutor

      ThreadPoolExecutor

    • Scheduler

      There is usually only one scheduler in an application , Application developers usually don't deal with job storage directly 、 Scheduler and trigger , Instead, the scheduler provides the appropriate interface to handle these , Configuring job storage and executors can be done in the scheduler , Such as adding 、 modify 、 Remove the job .

      • Configure the scheduler

        APScheduler There are many different ways to configure the scheduler , You can use a configuration dictionary as a parameter keyword to pass in , You can also create a scheduler first , Reconfigure and add jobs , This gives you more flexibility in different environments .

        from apscheduler.schedulers.blocking import BlockingScheduler
        from datetime import datetime
        def printTime():
        print(datetime.now().strftime(r'%Y-%m-%d'))
        # establish BlockingScheduler
        scheduler = BlockingScheduler()
        scheduler.add_job(printTime, 'interval', seconds=2)
        scheduler.start()
        

        The above code creates a BlockingScheduler, Default storage and default actuator are used by default (MemoryJobStore and T hreadPoolExecutor, The maximum number of threads in the thread pool <= 10), After configuration, use start() start-up

      • Configuration database storage

        from datetime import datetime
        from pymongo import MongoClient
        from apscheduler.schedulers.blocking import BlockingScheduler
        from apscheduler.jobstores.memory import MemoryJobStore
        from apscheduler.jobstores.mongodb import MongoDBJobStore
        from apscheduler.executors.pool import ThreadPoolExecutor, ProcessPoolExecutor
        # MongoDB Parameters 
        host = '127.0.0.1'
        port = 27017
        client = MongoClient(host, port)
        # Output time 
        def job():
        print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
        # storage 
        jobstores = {
        
        'mongo': MongoDBJobStore(collection='job', database='test', client=client),
        'default': MemoryJobStore()
        }
        # actuator 
        executors = {
        
        'default': ThreadPoolExecutor(10),
        'processpool': ProcessPoolExecutor(3)
        }
        job_defaults = {
        
        'coalesce': False,
        'max_instances': 3
        }
        scheduler = BlockingScheduler(jobstores=jobstores, executors=executors, job_defaults=job_defaults)
        scheduler.add_job(job, 'interval', seconds=5, jobstore='mongo')
        scheduler.start()
        
    • APSchduler Yes Job The relevant operation

      • add to job

        • add_job()

        • scheduler_job()

          Be careful ️: This method applies only to applications that do not change during runtime job, and add_job() Return to one apscheduler.job.Job example , Can be used to change or remove job

      • remove job

        • remove_job(): Use jobId remove job

        • job.remove(): Use add_job() The returned instance

          from apscheduler.schedulers.blocking import BlockingScheduler
          from datetime import datetime
          def printTime():
          print(datetime.now().strftime(r'%Y-%m-%d'))
          scheduler = BlockingScheduler()
          job = scheduler.add_job(printTime, 'interval', seconds=2, id='remove_id')
          # Mode one 
          job.remove()
          # Mode two 
          scheduler.remove_job('remove_id')
          
      • Pause and resume job

        • pasue( Suspend task :add_job() The returned instance job.pasue())
        • resume( Recovery task :add_job() The returned instance job.resume())
        from apscheduler.schedulers.blocking import BlockingScheduler
        from datetime import datetime
        def printTime():
        print(datetime.now().strftime(r'%Y-%m-%d'))
        scheduler = BlockingScheduler()
        job = scheduler.add_job(printTime, 'interval', seconds=2, id='remove_id')
        # Suspend task 
        job.pause()
        # Recovery task 
        job.resume()
        scheduler.start()
        

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved