File tree Expand file tree Collapse file tree 1 file changed +22
-5
lines changed Expand file tree Collapse file tree 1 file changed +22
-5
lines changed Original file line number Diff line number Diff line change 3
3
Polling timers for functions.
4
4
Registers timers and performs run once or periodical function execution after defined time intervals.
5
5
"""
6
+ # select.select call used as polling waiter where it is possible
7
+ # cause time.sleep sometimes may load CPU up to 100% with small polling wait interval
6
8
try :
7
- import utime as time
8
- import uselect as select
9
- except ImportError :
9
+ # cpython
10
10
import time
11
11
import select
12
12
13
+ polling_wait = lambda x : select .select ([], [], [], x )
14
+ polling_wait (0.01 )
15
+ except OSError :
16
+ # windows case where select.select call fails
17
+ polling_wait = lambda x : time .sleep (x )
18
+
19
+ except ImportError :
20
+ # micropython
21
+ import utime as time
22
+
23
+ try :
24
+ from uselect import select as s_select
25
+
26
+ polling_wait = lambda x : s_select ([], [], [], x )
27
+ except ImportError :
28
+ # case when micropython port does not support select.select
29
+ polling_wait = lambda x : time .sleep (x )
30
+
13
31
WAIT_SEC = 0.05
14
32
MAX_TIMERS = 16
15
33
DEFAULT_INTERVAL = 10
@@ -63,8 +81,7 @@ def get_timers(self):
63
81
return {k : states [v .stopped ] for k , v in self .timers .items ()}
64
82
65
83
def run (self ):
66
- # select call used cause time.sleep loads CPU up to 100% with small polling time
67
- select .select ([], [], [], WAIT_SEC )
84
+ polling_wait (WAIT_SEC )
68
85
timers_intervals = [curr_timer .run () for curr_timer in Timer .timers .values () if not curr_timer .stopped ]
69
86
if not timers_intervals and self .no_timers_err :
70
87
raise TimerError ('Running timers not found' )
You can’t perform that action at this time.
0 commit comments