File: kronos.cpython-34.pyc

package info (click to toggle)
streamtuner2 2.2.1%2Bdfsg-2.1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 2,432 kB
  • sloc: python: 8,976; javascript: 6,560; makefile: 91; php: 51; sh: 7; perl: 3
file content (153 lines) | stat: -rw-r--r-- 20,187 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153

	(UyM@sHdZddlmZdZdddddd	d
ddd
dddddddddddddddgZddlZddlZddlZddlZddl	Z	ddl
Z
GdddZGdd
d
ZGd ddZ
Gd!dde
ZGd"dde
ZGd#ddZGd$ddee
ZGd%ddee
ZyddlZGd&ddeZGd'ddZGd(ddeeZGd)ddeeZGd*ddeeZGd+ddeeZWnek
rYnXeed,rddlZGd-ddeZGd.d	d	ZGd/ddeeZGd0ddeeZ Gd1d
d
eeZ!Gd2ddeeZ"ne#d3krDd4d5Z$eZ%e%j&e$d6dd7ej'd8gde%j(e)d9ej*d:e)d;e%j+e)d<ndS)=aModule that provides a cron-like task scheduler.

This task scheduler is designed to be used from inside your own program.
You can schedule Python functions to be called at specific intervals or
days. It uses the standard 'sched' module for the actual task scheduling,
but provides much more:

* repeated tasks (at intervals, or on specific days)
* error handling (exceptions in tasks don't kill the scheduler)
* optional to run scheduler in its own thread or separate process
* optional to run a task in its own thread or separate process

If the threading module is available, you can use the various Threaded
variants of the scheduler and associated tasks. If threading is not
available, you could still use the forked variants. If fork is also
not available, all processing is done in a single process, sequentially.

There are three Scheduler classes:

    Scheduler    ThreadedScheduler    ForkedScheduler

You usually add new tasks to a scheduler using the add_interval_task or
add_daytime_task methods, with the appropriate processmethod argument
to select sequential, threaded or forked processing. NOTE: it is impossible
to add new tasks to a ForkedScheduler, after the scheduler has been started!
For more control you can use one of the following Task classes
and use schedule_task or schedule_task_abs:

    IntervalTask    ThreadedIntervalTask    ForkedIntervalTask
    SingleTask      ThreadedSingleTask      ForkedSingleTask 
    WeekdayTask     ThreadedWeekdayTask     ForkedWeekdayTask
    MonthdayTask    ThreadedMonthdayTask    ForkedMonthdayTask

Kronos is the Greek God of Time.

Kronos scheduler (c) Irmen de Jong.
This version has been extracted from the Turbogears source repository
and slightly changed to be completely stand-alone again. Also some fixes
have been made to make it work on Python 2.6 (sched module changes).
The version in Turbogears is based on the original stand-alone Kronos.
This is open-source software, released under the MIT Software License:
http://www.opensource.org/licenses/mit-license.php

)print_functionz2.1DayTaskReschedulerForkedIntervalTaskForkedMonthdayTaskForkedSchedulerForkedSingleTaskForkedTaskMixinForkedWeekdayTaskIntervalTaskMonthdayTask	Scheduler
SingleTaskTaskThreadedIntervalTaskThreadedMonthdayTaskThreadedSchedulerThreadedSingleTaskThreadedTaskMixinThreadedWeekdayTaskWeekdayTaskadd_interval_taskZadd_monthday_taskadd_single_taskZadd_weekday_taskcancelmethodNc@s"eZdZdZdZdZdS)r
sequentialforkedthreadedN)__name__
__module____qualname__rrrr r ./bundle/kronos.pyrTsc@seZdZdZddZddZddZdd	Zd
dZdd
Z	ddZ
ddZddZddZ
ddZddZejd%krddZddZnd dZd!dZd"d#Zd$S)&rzThe Scheduler itself.cCs(d|_tjtj|j|_dS)NT)runningsched	schedulertime_Scheduler__delayfunc)selfr r r!__init__\s	zScheduler.__init__cCs|dkrtj|n|j}tj|}d}||}x>|jr|tjkr|j|krtj|qKW|js|j|krdStj}||krtj||ndS)N
)r%sleep_getqueuetoptimer")r'delayZtoptimeendtimeperiodZstoptimenowr r r!Z__delayfunc`s
zScheduler.__delayfunccCsdS)Nr )r'r r r!
_acquire_lockuszScheduler._acquire_lockcCsdS)Nr )r'r r r!
_release_lockxszScheduler._release_lockc
Cs|dks|dkr'tdn|tjkr?t}n<|tjkrWt}n$|tjkrot}ntd|sg}n|si}n||||||}	|j|	||	S)aAdd a new Interval Task to the schedule.
        
        A very short initialdelay or one of zero cannot be honored, you will 
        see a slight delay before the task is first executed. This is because
        the scheduler needs to pick it up in its loop.

        rzDelay or interval must be >0zInvalid processmethod)	
ValueErrorrrr
rrrr
schedule_task)
r'actiontasknameinitialdelayinterval
processmethodargskw	TaskClasstaskr r r!r{s 						zScheduler.add_interval_taskc	Cs|dkrtdn|tjkr3t}n<|tjkrKt}n$|tjkrct}ntd|s~g}n|si}n|||||}|j|||S)z@Add a new task to the scheduler that will only be executed once.rzDelay must be >0zInvalid processmethod)	r4rrr
rrrrr5)	r'r6r7r8r:r;r<r=r>r r r!rs 					zScheduler.add_single_taskc	CsL|r|rtdn|s*g}n|s9i}n|r|tjkrWt}	n<|tjkrot}	n$|tjkrt}	ntd|	||||||}
n|r)|tjkrt}	n<|tjkrt	}	n$|tjkrt
}	ntd|	||||||}
n|
jd}|j|
||
S)z9Add a new Day Task (Weekday or Monthday) to the schedule.z4You can only specify weekdays or monthdays, not bothzInvalid processmethodT)
r4rrrrrrr	rrrget_schedule_timeschedule_task_abs)r'r6r7weekdays	monthdays	timeondayr:r;r<r=r>Z	firsttimer r r!add_daytime_tasks6								zScheduler.add_daytime_taskcCs|jrR|jz.|jj|d|tj|f|_Wd|jXn*|jj|d|tj|f|_dS)zAdd a new task to the scheduler with the given delay (seconds).
        
        Low-level method for internal use.
        
        rN)r"r1r#enterweakrefrefeventr2)r'r>r-r r r!r5s	
zScheduler.schedule_taskcCs|jrR|jz.|jj|d|tj|f|_Wd|jXn*|jj|d|tj|f|_dS)zAdd a new task to the scheduler for the given absolute time value.
        
        Low-level method for internal use.
        
        rN)r"r1r#ZenterabsrFrGrHr2)r'r>abstimer r r!r@s	
zScheduler.schedule_task_abscCs|jdS)zStart the scheduler.N)_run)r'r r r!startszScheduler.startcCsd|_|jdS)z0Remove all pending tasks and stop the Scheduler.FN)r"_clearschedqueue)r'r r r!stops	zScheduler.stopcCs|jj|jdS)zCancel given scheduled task.N)r#rrH)r'r>r r r!rszScheduler.cancelcCs|jjdjS)Nr)r#_queuer%)r'r r r!r,szScheduler._getqueuetoptimecCsg|jjdd<dS)N)r#rP)r'r r r!rLszScheduler._clearschedqueuecCs|jjddS)Nr)r#queue)r'r r r!r,scCsg|jjdd<dS)N)r#rQ)r'r r r!rLscCsx|jry|jjWnytk
r}zYtd|dtjtdjtj	tj
dtjtddtjWYdd}~XnX|jrtjdqqWdS)Nz ERROR DURING SCHEDULER EXECUTIONfile-r*z--------------------)
r"r#run	Exceptionprintsysstderrjoin	tracebackformat_exceptionexc_infor%r+)r'xr r r!rJs	"&	zScheduler._runN)rNrO)rrr__doc__r(r&r1r2rrrDr5r@rKrMrrYversion_infor,rLrJr r r r!rYs&&c@sReZdZdZddZddZddZdd	Zd
dZdS)
rz*Abstract base class of all scheduler taskscCs(||_||_||_||_dS)zThis is an abstract class!N)namer6r;r<)r'rbr6r;r<r r r!r(#s			z
Task.__init__cCsWy|jWn2tk
rB}z|j|WYdd}~XnX|j|dS)z2Execute the task action in the scheduler's thread.N)executerWhandle_exception
reschedule)r'schedulerrefr_r r r!__call__*s
 z
Task.__call__cCstddS)z8This method should be defined in one of the sub classes!zIYou're using the abstract base class 'Task', use a concrete class insteadN)NotImplementedError)r'r$r r r!re2szTask.reschedulecCs|j|j|jdS)zExecute the actual task.N)r6r;r<)r'r r r!rc7szTask.executecCsXtd|dtjtdjtjtjdtjtddtjdS)z8Handle any exception that occured during task execution.zERROR DURING TASK EXECUTIONrRrSrTrUNz--------------------)rXrYrZr[r\r]r^)r'excr r r!rd;s+zTask.handle_exceptionN)	rrrr`r(rgrercrdr r r r!r sc@s"eZdZdZddZdS)r
zA task that only runs once.cCsdS)Nr )r'r$r r r!reEszSingleTask.rescheduleN)rrrr`rer r r r!r
Bsc@s4eZdZdZddddZddZdS)r
z>A repeated task that occurs at certain intervals (in seconds).NcCs&tj|||||||_dS)N)rr(r9)r'rbr9r6r;r<r r r!r(LszIntervalTask.__init__cCs|j||jdS)z<Reschedule this task according to its interval (in seconds).N)r5r9)r'r$r r r!rePszIntervalTask.reschedule)rrrr`r(rer r r r!r
Isc@s:eZdZdZddZddZddZdS)	rzBA mixin class that contains the reschedule logic for the DayTasks.cCs
||_dS)N)rC)r'rCr r r!r(XszDayTaskRescheduler.__init__cCsttj}|rI|d|df|jkrW|dd7<qWn|dd7<|j\|d<|d<d|d<tj|S)z?Calculate the time value at which this task is to be scheduled.rNr3rr*)listr%	localtimerCmktime)r'todayr0r r r!r?[s
z$DayTaskRescheduler.get_schedule_timecCs#|jd}|j||dS)zReschedule this task according to the daytime for the task.
        
        The task is scheduled for tomorrow, for the given daytime.

        FN)r?r@)r'r$rIr r r!relszDayTaskRescheduler.rescheduleN)rrrr`r(r?rer r r r!rUsc@s4eZdZdZddddZddZdS)rzaA task that is called at specific days in a week (1-7), at a fixed time
    on the day.
    
    NcCst|ttfks*t|dkr9tdnt|ttfkr`tdntj||tj|||||||_dS)NrNz)timeonday must be a 2-tuple (hour,minute)z@weekdays must be a sequence of weekday numbers 1-7 (1 is Monday))	typerltuplelen	TypeErrorrr(rdays)r'rbrArCr6r;r<r r r!r(~s*zWeekdayTask.__init__cCs?tjjd}||jkr;|j|j|jndS)Nr3)r%rmtm_wdayrtr6r;r<)r'weekdayr r r!rcszWeekdayTask.execute)rrrr`r(rcr r r r!rxs
c@s4eZdZdZddddZddZdS)rzdA task that is called at specific days in a month (1-31), at a fixed 
    time on the day.
    
    NcCst|ttfks*t|dkr9tdnt|ttfkr`tdntj||tj|||||||_dS)NrNz)timeonday must be a 2-tuple (hour,minute)z6monthdays must be a sequence of monthdays numbers 1-31)	rprlrqrrrsrr(rrt)r'rbrBrCr6r;r<r r r!r(s*zMonthdayTask.__init__cCs5tjj|jkr1|j|j|jndS)N)r%rmtm_mdayrtr6r;r<)r'r r r!rcszMonthdayTask.execute)rrrr`r(rcr r r r!rs
c@sReZdZdZddZddZddZdd	Zd
dZdS)
rz(A Scheduler that runs in its own thread.cCs tj|tj|_dS)N)rr(	threadingLock_lock)r'r r r!r(s
zThreadedScheduler.__init__cCs9tjd|j|_|jjd|jjdS)z4Splice off a thread in which the scheduler will run.targetTN)rxThreadrJthread	setDaemonrK)r'r r r!rKszThreadedScheduler.startcCs7tj|y|jjWntk
r2YnXdS)z5Stop the scheduler and wait for the thread to finish.N)rrMr}r[AttributeError)r'r r r!rMs


zThreadedScheduler.stopcCs|jjdS)zLock the thread's task queue.N)rzacquire)r'r r r!r1szThreadedScheduler._acquire_lockcCs|jjdS)z,Release the lock on th ethread's task queue.N)rzrelease)r'r r r!r2szThreadedScheduler._release_lockN)	rrrr`r(rKrMr1r2r r r r!rsc@s.eZdZdZddZddZdS)rz:A mixin class to make a Task execute in a separate thread.cCs-tjd|jj|j|dS)z*Execute the task action in its own thread.r{N)rxr|threadedcallrKre)r'rfr r r!rgszThreadedTaskMixin.__call__cCsGy|jWn2tk
rB}z|j|WYdd}~XnXdS)N)rcrWrd)r'r_r r r!rszThreadedTaskMixin.threadedcallN)rrrr`rgrr r r r!rsc@seZdZdZdS)rz.Interval Task that executes in its own thread.N)rrrr`r r r r!rsc@seZdZdZdS)rz,Single Task that executes in its own thread.N)rrrr`r r r r!rsc@seZdZdZdS)rz-Weekday Task that executes in its own thread.N)rrrr`r r r r!rsc@seZdZdZdS)rz.Monthday Task that executes in its own thread.N)rrrr`r r r r!rsforkc@sFeZdZdZddZddZddZdd	Zd
S)rz0A Scheduler that runs in its own forked process.cCs,t|dr(tj|jtjndS)Nchildpid)hasattroskillrsignalSIGKILL)r'r r r!__del__szForkedScheduler.__del__cCs[tj}|dkrHtjtj|j|jtjdn||_|`dS)z7Fork off a new process in which the scheduler will run.rN)	rrrSIGUSR1
signalhandlerrJ_exitrr#)r'pidr r r!rKs
	zForkedScheduler.startcCs-tj|jtjtj|jddS)z6Stop the scheduler and wait for the process to finish.rN)rrrrrwaitpid)r'r r r!rMszForkedScheduler.stopcCstj|dS)N)rrM)r'sigstackr r r!rszForkedScheduler.signalhandlerN)rrrr`rrKrMrr r r r!rs
c@s"eZdZdZddZdS)rz;A mixin class to make a Task execute in a separate process.cCstj}|dkrky|jWn2tk
rZ}z|j|WYdd}~XnXtjdn|j|dS)z+Execute the task action in its own process.rN)rrrcrWrdrre)r'rfrr_r r r!rgs zForkedTaskMixin.__call__N)rrrr`rgr r r r!rsc@seZdZdZdS)rz/Interval Task that executes in its own process.N)rrrr`r r r r!r!sc@seZdZdZdS)rz-Single Task that executes in its own process.N)rrrr`r r r r!r%sc@seZdZdZdS)r	z.Weekday Task that executes in its own process.N)rrrr`r r r r!r	)sc@seZdZdZdS)rz/Monthday Task that executes in its own process.N)rrrr`r r r r!r-s__main__cCs.td|dtjdtd|dS)Nz>>>TASKzsleeping 3 secondsrjz<<<END_TASK)rXr%r+)argr r r!
testaction4s
rz
test action 1rkztask 1z%Scheduler started, waiting 15 sec....zSTOP SCHEDULERZEXITING),r`
__future__r__version____all__rrYr#r%r\rFrrrr
r
rrrrxrrrrrrImportErrorrrrrrrr	rrrsrrrKrXr+rMr r r r!<module>,s	"#
	%