Python Timer Service Module

Source: Internet
Author: User

The Python timer task uses the following methods:

Import= Sched.scheduler (time.time, Time.sleep) shelder.enter (2, 0, Print_time, ()) Shelder.run ()

The order of execution is described below:

1. Create an instance of a timed task execution 2, insert a scheduled task into the execution Queue 3, start a timing task parameter description of the Enter method: the first parameter is the number of seconds after the task is started, the second parameter is the task priority the third parameter is the method to be executed the fourth parameter is the method to pass in the parameter, If not, use it directly () Example 1: Sequential execution
Import TimeImportscheddefprint_time ():Print 'Now the time is :', Time.time ()defdo_some_times ():Print 'Begin Time:', Time.time () Shelder.enter (2, 0, Print_time, ()) Shelder.enter (2, 0, Print_time, ()) Shelder.enter (5, 1, Print_time, ()) Shelder.run ()Print 'End Time:', Time.time () do_some_times ()

Operation Result:

Begin time:1525682354.85

Now the time is:1525682356.86

Now the time is:1525682356.86

Now the time is:1525682359.86

End time:1525682359.86

The time behind here is the same because it means adding to the queue time.

In the case of multithreading, the use of the above method will introduce thread safety restrictions, the official manual also made a full description, as follows:

In multi-threaded environments, the scheduler class have limitations with respect to thread-safety, inability to insert a n EW task before the one currently pending in a running scheduler, and holding up the main thread until the event queue is E Mpty. Instead, the preferred approach is to use the threading. Timer class instead. In the end, it means using threading's timer instead. Example 2: Thread-safe
 fromThreadingImportTimerImport Timedefprint_time ():Print 'Now the time is :', Time.time ()defprint_times ():Print 'begin Time is:', Time.time () Timer (5, Print_time, (())). Start () Timer (10, Print_time, (()). Start () Time.sleep (2)    Print 'End Time is:', Time.time () print_times ()

Operation Result:

Begin Time is:1525682440.55

End Time is:1525682442.55

Now the time is:1525682445.55

Now the time is:1525682450.55

Example 3: Task self-scheduling

 fromThreadingImportTimerImportTimecounttimes=3defprint_time ():GlobalCounttimesifCounttimes >0:Print 'Now the time is %d,%f:'%(Counttimes,time.time ()) Timer (5, Print_time, (())). Start () Counttimes-= 1defprint_times ():Print 'begin Time is:', Time.time () Timer (5, Print_time, (()). Start () Time.sleep (2)    Print 'End Time is:', Time.time () print_times ()

Operation Result:

Begin Time is:1525682594.3

End Time is:1525682596.3

Now the time is 3,1525682599.300889:

Now the time is 2, 1525682604.302403:

Now the time is 1, 1525682609.302912:

Appendix

Common Schelder methods:

Scheduler.enterabs (Time, priority, action, argument) Scheduler.enter (delay, priority, action, argument) Scheduler.cancel (event) deletes a task event Scheduler.empty () The task queue is empty scheduler.run () starts a task and waits for the next task to scheduler.queue the task queue Reference Documentation:Https://docs.python.org/2/library/sched.html

Python Timer Service Module

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.