-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathmytimer.py
More file actions
executable file
·116 lines (92 loc) · 3.2 KB
/
Copy pathmytimer.py
File metadata and controls
executable file
·116 lines (92 loc) · 3.2 KB
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
#!/usr/bin/python
##################
# mytimer.py
#
# Copyright David Baddeley, 2009
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##################
import wx
import time
from wx.core import TIMER_CONTINUOUS
import logging
logger = logging.getLogger(__name__)
class MultiTargetTimer(wx.Timer):
"""
Timer which calls multiple handlers
"""
def __init__(self, PROFILE=False):
wx.Timer.__init__(self)
self.WantNotification = []
self.PROFILE=PROFILE
self.times = {}
def Notify(self):
for a in self.WantNotification:
ts = time.time()
try:
a()
except:
logger.exception('Error in timer callback')
if self.PROFILE:
te = time.time() - ts
ar = repr(a)
#if ar in times.keys():
# self.times[ar] = self.times[ar]+ te
#else:
self.times[ar] = te
def register_callback(self, callback):
self.WantNotification.append(callback)
def unregister_callback(self, callback):
self.WantNotification.remove(callback)
def start(self, delay_ms, single_shot=False):
if single_shot:
wx.CallAfter(self.Start, delay_ms, wx.TIMER_ONE_SHOT)
else:
wx.CallAfter(self.Start, delay_ms, wx.TIMER_CONTINUOUS)
mytimer=MultiTargetTimer #alias for backwards compatibility
class SingleTargetTimer(wx.Timer):
"""
Single shot timer which only calls a single function when it fires
This is a very light-weight wrapper around wx.Timer to permit consistent usage with non GUI timers.
"""
def __init__(self, target):
wx.Timer.__init__(self)
self._target = target
def Notify(self):
self._target()
def start(self, delay_ms, single_shot=False):
if single_shot:
wx.CallAfter(self.Start, delay_ms, wx.TIMER_ONE_SHOT)
else:
wx.CallAfter(self.Start, delay_ms, wx.TIMER_CONTINUOUS)
def stop(self):
self.Stop()
def call_in_main_thread(callable, *args, **kwargs):
"""
Alias to permit different frontends
Parameters
----------
callable
args
kwargs
Returns
-------
"""
wx.CallAfter(callable, *args, **kwargs)
def loop_forever():
'''Dummy loop forever (real event loop is in wx)'''
while (wx.GetApp().IsMainLoopRunning()):
time.sleep(1)