8000 added handling for cases when select.select not supported · fgervais/lib-python@f928934 · GitHub
[go: up one dir, main page]

Skip to content

Commit f928934

Browse files
author
amorozenko
committed
added handling for cases when select.select not supported
1 parent b9822e7 commit f928934

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

blynktimer.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,31 @@
33
Polling timers for functions.
44
Registers timers and performs run once or periodical function execution after defined time intervals.
55
"""
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
68
try:
7-
import utime as time
8-
import uselect as select
9-
except ImportError:
9+
# cpython
1010
import time
1111
import select
1212

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+
1331
WAIT_SEC = 0.05
1432
MAX_TIMERS = 16
1533
DEFAULT_INTERVAL = 10
@@ -63,8 +81,7 @@ def get_timers(self):
6381
return {k: states[v.stopped] for k, v in self.timers.items()}
6482

6583
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)
6885
timers_intervals = [curr_timer.run() for curr_timer in Timer.timers.values() if not curr_timer.stopped]
6986
if not timers_intervals and self.no_timers_err:
7087
raise TimerError('Running timers not found')

0 commit comments

Comments
 (0)
0