Python performs timed tasks __python

Source: Internet
Author: User

One, when you want a method to be executed after a few seconds or even longer, you can do this:

Import time
from threading import Timer

def print_time (enter_time):
  print ' Now ', Time.time (), enter_the _box_time is ", Enter_time


print time.time ()
timer (5,  Print_time, (Time.time (),)). Start ()
timer (10, Print_time, (Time.time (),)). Start ()
print time.time ()

In this case, the Print_time method is executed once from the program to 5, second, and 10 seconds.

Second, when you want to let one of your methods to perform in a certain period of time, this requires the framework of the timing task Apscheduler, the introduction of the online installation of Apscheduler is simple:

Easy_install Apscheduler on the line, unfortunately I might use the reason for Python 2.7, although the installation was prompted to succeed, but write code to introduce these packages always have problems, and finally

I found a apscheduler2.0 software package, unpack and switch to the corresponding directory after the Python setup.py install to solve the problem, this package in my resources can be downloaded.

Then you can start writing code, the code implementation style has two:

1, with the modifier of the wording:

From Apscheduler.scheduler Import Scheduler
import datetime
Schedudler = Scheduler (Daemonic = False)

@ Schedudler.cron_schedule (second= ', day_of_week= ' 0-7 ', hour= ' 9-12,13-16 ')
def quote_send_sh_job ():
    print ' A simple cron job start at ', Datetime.datetime.now ()

Schedudler.start ()

2, without the modifier of the wording:

def cornstart (self,event):
         Schedudler = Scheduler (Daemonic = True)

         schedudler.add_cron_job (Self.timing_exe, day_of_week= ' Mon-sun ', hour= ' 0-12 ', minute= ' 0-59 ', second= ', '
         print ' get start '
         Schedudler.start ()

The

should be well understood, Timing_exe is the name of the function to execute, and if the function has parameters that can be added to a args[]

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.