Python timed execution method

Source: Internet
Author: User

1 time.sleep

Import time

For I in range (5):

Print (i)

Time.sleep (10)

2 with Shed

Import TimeImportsched Schedule=Sched.scheduler (Time.time, Time.sleep)deffunc (STRING1,FLOAT1):Print(" now is", Time.time (),"| output=", STRING1,FLOAT1)Print(Time.time ()) Schedule.enter (2,0,func, ("test1", Time.time ())) Schedule.enter (2,0,func, ("test1", Time.time ())) Schedule.enter (3,0,func, ("test1", Time.time ())) Schedule.enter (4,0,func, ("test1", Time.time ())) Schedule.run ()Print(Time.time ())

Where Func is put in the function to execute, with Schedule.enter to add the function to execute, the first parameter inside is the time to delay execution, initialized with Sched.scheduler

1512033155.9311035 is 1512033157.9316308  | output= test1 1512033155.9311035is 1512033157.9316308  | output= test1 1512033155.9311035 is 1512033158.9322016  | output= Test1 1512033155.9311035 is 1512033159.9316351  | output= test1 1512033155.93110351512033159.9316351 in 4.2s]

Above is the result of execution, the disadvantage is that the task queue is blocking type, that is, the task in schedule is not finished, the following main thread will not execute

3 with the timer in the threading, realize the non-blocking type, that is, the main thread to perform the task simultaneously

Import Time fromThreadingImportTimerdefPrint_time (enter_time):Print " now is", Time.time (),"Enter_the_box_time is", Enter_timePrinttime.time () Timer (5, Print_time, (Time.time (),)). Start () Timer (10, Print_time, (Time.time (),)). Start ()PrintTime.time ()

Execution Result:

1512034286.94431691512034286.9452875is are 1512034286.9443169 is  is 1512034286.9452875 in 10.2s]

It can be seen that the task and the main thread is synchronous execution, but the latter 3 bit is slightly different, it should be Python multithreading is not true multithreading results

Perform tasks on a regular basis at some time every day:

ImportdatetimeImport Timedefdosth ():Print('Test')    #it takes a minute to pretend to do this thingTime.sleep (60)defMain (h=0, m=0):" "h = Set hour, m for set minutes" "     whileTrue:#determine if the set time is reached, such as 0:00         whileTrue:now=Datetime.datetime.now ()#time to set, end inner loop            ifNow.hour==h andnow.minute==m: Break            #wait 20 seconds, and then test again in less time .Time.sleep (20)        #Do the right thing, do it once a dayDosth () main ()

Python timed execution method

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.