Movatterモバイル変換


[0]ホーム

URL:


Navigation

17.6.sched — Event scheduler

Source code:Lib/sched.py


Thesched module defines a class which implements a general purpose eventscheduler:

classsched.scheduler(timefunc=time.monotonic,delayfunc=time.sleep)

Thescheduler class defines a generic interface to scheduling events.It needs two functions to actually deal with the “outside world” —timefuncshould be callable without arguments, and return a number (the “time”, in anyunits whatsoever). If time.monotonic is not available, thetimefunc defaultis time.time instead. Thedelayfunc function should be callable with oneargument, compatible with the output oftimefunc, and should delay that manytime units.delayfunc will also be called with the argument0 after eachevent is run to allow other threads an opportunity to run in multi-threadedapplications.

Changed in version 3.3:timefunc anddelayfunc parameters are optional.

Changed in version 3.3:scheduler class can be safely used in multi-threadedenvironments.

Example:

>>>importsched,time>>>s=sched.scheduler(time.time,time.sleep)>>>defprint_time(a='default'):...print("From print_time",time.time(),a)...>>>defprint_some_times():...print(time.time())...s.enter(10,1,print_time)...s.enter(5,2,print_time,argument=('positional',))...s.enter(5,1,print_time,kwargs={'a':'keyword'})...s.run()...print(time.time())...>>>print_some_times()930343690.257From print_time 930343695.274 positionalFrom print_time 930343695.275 keywordFrom print_time 930343700.273 default930343700.276

17.6.1. Scheduler Objects

scheduler instances have the following methods and attributes:

scheduler.enterabs(time,priority,action,argument=(),kwargs={})

Schedule a new event. Thetime argument should be a numeric type compatiblewith the return value of thetimefunc function passed to the constructor.Events scheduled for the sametime will be executed in the order of theirpriority.

Executing the event means executingaction(*argument,**kwargs).argument is a sequence holding the positional arguments foraction.kwargs is a dictionary holding the keyword arguments foraction.

Return value is an event which may be used for later cancellation of the event(seecancel()).

Changed in version 3.3:argument parameter is optional.

New in version 3.3:kwargs parameter was added.

scheduler.enter(delay,priority,action,argument=(),kwargs={})

Schedule an event fordelay more time units. Other than the relative time, theother arguments, the effect and the return value are the same as those forenterabs().

Changed in version 3.3:argument parameter is optional.

New in version 3.3:kwargs parameter was added.

scheduler.cancel(event)

Remove the event from the queue. Ifevent is not an event currently in thequeue, this method will raise aValueError.

scheduler.empty()

Return true if the event queue is empty.

scheduler.run(blocking=True)

Run all scheduled events. This method will wait (using thedelayfunc()function passed to the constructor) for the next event, then execute it and soon until there are no more scheduled events.

Ifblocking is false executes the scheduled events due to expire soonest(if any) and then return the deadline of the next scheduled call in thescheduler (if any).

Eitheraction ordelayfunc can raise an exception. In either case, thescheduler will maintain a consistent state and propagate the exception. If anexception is raised byaction, the event will not be attempted in future callstorun().

If a sequence of events takes longer to run than the time available before thenext event, the scheduler will simply fall behind. No events will be dropped;the calling code is responsible for canceling events which are no longerpertinent.

New in version 3.3:blocking parameter was added.

scheduler.queue

Read-only attribute returning a list of upcoming events in the order theywill be run. Each event is shown as anamed tuple with thefollowing fields: time, priority, action, argument, kwargs.

Table Of Contents

Previous topic

17.5.subprocess — Subprocess management

Next topic

17.7.queue — A synchronized queue class

This Page

Quick search

Enter search terms or a module, class or function name.

Navigation

©Copyright 1990-2017, Python Software Foundation.
The Python Software Foundation is a non-profit corporation.Please donate.
Last updated on Sep 19, 2017.Found a bug?
Created usingSphinx 1.2.

[8]ページ先頭

©2009-2025 Movatter.jp