-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrepeatedTimer.py
executable file
·63 lines (53 loc) · 1.89 KB
/
repeatedTimer.py
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (C)2018 NavInfo Co.,Ltd. All right reserved.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from time import sleep
from threading import Timer
from performanceInfo import PerformanceInfo
class RepeatedTimer:
def __init__(self, interval, *args, **kwargs):
self.__timer = None
self.__interval = interval
self.__procName = args[0]
self.__procInfo = PerformanceInfo(self.__procName)
self.__procStart = self.__procInfo.start
self.__procStop = self.__procInfo.stop
self.__kwargs = kwargs
self.__is_running = False
self.start()
def __run(self):
self.__is_running = False
self.start()
self.__procStart()
def start(self):
if not self.__is_running:
self.__timer = Timer(self.__interval, self.__run)
self.__timer.start()
self.__is_running = True
def stop(self):
self.__timer.cancel()
self.__is_running = False
return self.__procStop()
if __name__ == '__main__':
print("starting...")
rt1 = RepeatedTimer(1, "server_manager")
rt2 = RepeatedTimer(2, "java")
rt3 = RepeatedTimer(1, None)
try:
sleep(5)
finally:
result1 = rt1.stop()
result2 = rt2.stop()
result3 = rt3.stop()
print(result1)
print(result2)
print(result3)