|
1 # |
|
2 # Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 # All rights reserved. |
|
4 # |
|
5 # This program is free software: you can redistribute it and/or modify |
|
6 # it under the terms of the GNU Lesser General Public License as published by |
|
7 # the Free Software Foundation, either version 3 of the License, or |
|
8 # (at your option) any later version. |
|
9 # |
|
10 # This program is distributed in the hope that it will be useful, |
|
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 # GNU Lesser General Public License for more details. |
|
14 # |
|
15 # You should have received a copy of the GNU Lesser General Public License |
|
16 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17 |
|
18 # Implements a 'simple' watchdog built on top of the Timer class from |
|
19 # the Threading module. |
|
20 # After a specified interval a WatchDog will invoke a specified function. |
|
21 # At any point the WatchDog can be Reset, which means the countdown will start from scratch. |
|
22 # WatchDogs can also be Started and Stopped. |
|
23 # NB the WatchDog has no idea what the function it invokes will do. |
|
24 |
|
25 import threading |
|
26 |
|
27 class WatchDog(object): |
|
28 def __init__(self, interval, function): |
|
29 self.interval = interval |
|
30 self.function = function |
|
31 self.Watcher() |
|
32 |
|
33 def Watcher(self): |
|
34 self.timerThread = threading.Timer(self.interval, self.function) |
|
35 |
|
36 def Start(self): |
|
37 self.timerThread.start() |
|
38 |
|
39 def Stop(self): |
|
40 self.timerThread.cancel() |
|
41 |
|
42 def Reset(self): |
|
43 self.Stop() |
|
44 self.Watcher() |
|
45 self.Start() |
|
46 |