Python standard library notes (5)-sched module, pythonsched

Source: Internet
Author: User

Python standard library notes (5)-sched module, pythonsched
Event Scheduling

  schedThe module content is very simple and only defines one class. It is used as the most common event scheduling module.

  class sched.scheduler(timefunc, delayfunc)This class defines a common interface for scheduling events. It requires two parameters to be passed in externally,timefuncIs a function that returns time numbers without parameters (such as the time in the time module ),delayfuncIt should be a function that requires a parameter to call, be compatible with timefunc output, and act as a function that delays multiple time units (such as sleep of the time module ).

The following is a column:

Import sched, times = sched. scheduler (time. time, time. sleep) # generate the scheduler def print_time (): print "From print_time", time. time () def print_some_times (): print time. time () s. enter (5, 1, print_time, () # Add a scheduling event # The four parameters are: # The interval event (the specific value is determined by delayfunc, here is the second ); # priority (when two events arrive at the same time); # triggered functions; # function parameters; s. enter (10, 1, print_time, () # Run s. run () print time. time () if _ name _ = '_ main _': print_some_times ()

The output result shows that the first event is executed every five seconds, and the second event is executed every 10 seconds:

1499259731.99From print_time 1499259736.99From print_time 1499259741.991499259741.99

In multi-threaded scenarios, thread security issues occur. The run () function blocks the main thread. Officially recommendedthreading.TimerClass substitution:

Import timefrom threading import Timerdef print_time (): print "From print_time", time. time () def print_some_times (): print time. time () Timer (5, print_time ,()). start () Timer (10, print_time ,()). start () time. sleep (11) # block the main thread, wait until the scheduling program is executed, and then execute the following content print time. time () if _ name _ = '_ main _': print_some_times ()
Scheduler object Method

The scheduler object has the following methods or attributes:

  • Schedity. enterabs (time, priority, action, argument)

Add an event,timeThe parameter should be the same as the one passed to the constructor.timefuncA value type that is compatible with the return value of a function. Events that arrive at the same time will followprioritySequential execution.

The execution event is actually the executionaction(argument). Argument must beactionParameter sequence.

The return value is an event that can be used to cancel an event later (seecancel()).

  • Schedity. enter (delay, priority, action, argument)

Schedule an event to delaydelayTime unit. In addition to time, other parameters, meanings, and returned valuesenterabs(). Actually internalenterabsIs usedenterCall.

  • Schedcel. cancel (event)

Delete an event from the queue. If the event is not an event in the current queue, this method runsValueError.

  • Schedy. empty ()

Determines whether the queue is empty.

  • Scheduler. run ()

Run all scheduled events. This function will wait (usingdelayfunc()Function), and then execute the event until there is no scheduled event.

AnyactionOrdelayfuncCan cause exceptions. In both cases, the scheduler will maintain a consistent state and spread exceptions. If an exception is causedactionThe execution will not continue.run().

  • Schedue. queue

Read-only attribute, returns a list of events to be reached (sorted by arrival events), each event hastime,priority,action,argumentComposednamedtuple.

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.