A brief introduction of the processing time and the method of realizing the scheduled task in Python3

Source: Internet
Author: User
Tags timedelta
Regardless of which programming language, time is certainly a very important part, today to see how Python handles time and Python timing tasks, note: This article is the Python3 version of the implementation, in the Python2 version of the implementation of a slightly different, there is time to write another for everyone to distinguish.
1. Calculate Tomorrow and yesterday's date

#! /usr/bin/env python#coding=utf-8# get today, yesterday and tomorrow's date # introduce a datetime module import datetime #计算今天的时间today = Datetime.date.today () # Calculate yesterday's Time yesterday = Today-datetime.timedelta (days = 1) #计算明天的时间tomorrow = today + datetime.timedelta (day = 1) #打印这三个时间pr Int (Yesterday, today, tomorrow)

2. Calculate the last time

Method One:

#! /usr/bin/env python#coding=utf-8# calculates the last time # introduced Datetime,calendar two modules import Datetime,calendar Last_friday = Datetime.date.today () Oneday = Datetime.timedelta (days = 1) while  last_friday.weekday ()! = Calendar. FRIDAY:  Last_friday-= oneday  print (Last_friday.strftime ('%A,%d-%b-%y '))

Method two: Using modulo arithmetic to find the last Friday

#! /usr/bin/env python#coding=utf-8# with modulo operations, you can calculate the number of days to subtract at a time, and the previous Friday # also introduces Datetime,calendar two modules import datetime import Calendar  today = Datetime.date.today () target_day = Calendar. FRIDAY this_day = Today.weekday () Delta_to_target = (this_day-target_day)% 7last_friday = Today-datetime.timedelta (da ys = delta_to_target)  print (Last_friday.strftime ("%d-%b-%y"))

3. Calculate the total playback time of the song

#! /usr/bin/env python#coding=utf-8# Get a list of all the songs in the Play Time and import datetime  def Total_timer (Times):  td = Datetime.timedelta (0)  duration = SUM ([Datetime.timedelta (minutes = m, seconds = s) for M, s in times], TD)  return Duration  times1 = [(2, 3), (    3, 3),    ] Times2 = [(0, 5)    , (+ +), (4,), (1, 10), c11/>]  assert Total_timer (times1) = = Datetime.timedelta (0, 596) assert Total_timer (times2) = = Datetime.timedelta ( 0, 815)  print ("Tests passed.\n" "First  Test total:%s\n"  "Second Test total:%s"% (Total_timer (times1), t Otal_timer (Times2)))

4. Repeatedly execute a command

#! /usr/bin/env python#coding=utf-8# executes a command at the required interval  import time, OS  def re_exe (cmd, inc =): while  true:< C20/>os.system (cmd);   Time.sleep (inc)  Re_exe ("Echo%time%", 5)

5. Scheduled Tasks

#! /usr/bin/env python#coding=utf-8# here need to introduce three modules import time, OS, Sched  # The first parameter determines when a task is returned, returning the number of seconds from a specific time to the present experience # The second parameter measures time in some artificial way schedule = Sched.scheduler (Time.time, Time.sleep)  def perform_command (CMD, Inc):  Os.system (CMD)   def timming_exe (cmd, inc = $):  # Enter is used to schedule the occurrence of an event, starting from now on in nth Seconds  Schedule.enter (inc, 0, Perform_command, (CMD, INC)  # continues to run until the scheduled time queue becomes empty until  schedule.run ()    print ("Show hours after Seconds:") Timming_exe ("Echo%time%", 10)

6. Using Sched to implement periodic calls

#! /usr/bin/env python#coding=utf-8import time, OS, Sched  # The first parameter determines the duration of a task, and returns the number of seconds from a specific time to the present # The second parameter measures time in some artificial way Schedule = Sched.scheduler (Time.time, Time.sleep)  def perform_command (CMD, Inc):  # Schedule an inc second to run itself again, i.e. cycle run  Schedule.enter (inc, 0, Perform_command, (CMD, Inc))  Os.system (cmd)   def timming_exe (cmd, inc =):  # Enter is used to schedule the occurrence of an event, starting from now n seconds start  Schedule.enter (inc, 0, Perform_command, (CMD, Inc))  # Continue running until the scheduled time queue becomes empty  Schedule.run ()    print ("Show Time After Seconds:") Timming_exe ("Echo%time%", 10)
  • Related Article

    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.