Skip to content

Commit 1245f14

Browse files
activity.py: throttle async_call() to fix overflow
1 parent ce57c1e commit 1245f14

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

internal_filesystem/lib/mpos/app/activity.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
class Activity:
66

7+
throttle_async_call_counter = 0
8+
79
def __init__(self):
810
self.intent = None # Store the intent that launched this activity
911
self.result = None
@@ -14,12 +16,14 @@ def onCreate(self):
1416
pass
1517
def onStart(self, screen):
1618
pass
17-
def onResume(self, screen): # app gets foreground
19+
20+
def onResume(self, screen): # app goes to foreground
1821
self._has_foreground = True
19-
mpos.ui.th.add_event_cb(self.thc, 2)
22+
mpos.ui.th.add_event_cb(self.task_handler_callback, 1)
2023

2124
def onPause(self, screen): # app goes to background
2225
self._has_foreground = False
26+
2327
def onStop(self, screen):
2428
pass
2529
def onDestroy(self, screen):
@@ -65,9 +69,8 @@ def finish(self):
6569
def has_foreground(self):
6670
return self._has_foreground
6771

68-
def thc(self, a, b):
69-
#print("thc called")
70-
self.counter = 0
72+
def task_handler_callback(self, a, b):
73+
self.throttle_async_call_counter = 0
7174

7275
# Execute a function if the Activity is in the foreground
7376
def if_foreground(self, func, *args, **kwargs):
@@ -79,13 +82,13 @@ def if_foreground(self, func, *args, **kwargs):
7982
#print(f"[if_foreground] Skipped {func} because _has_foreground=False")
8083
return None
8184

82-
counter=0
8385
# Update the UI in a threadsafe way if the Activity is in the foreground
86+
# The call may get throttled, unless important=True is added to it.
8487
def update_ui_threadsafe_if_foreground(self, func, *args, important=False, **kwargs):
85-
self.counter += 1
86-
if not important and self.counter > 250:
87-
#print(f"skipping because self.counter is {self.counter} and not important")
88-
return
88+
self.throttle_async_call_counter += 1
89+
if not important and self.throttle_async_call_counter > 100: # 250 seems to be okay, so 100 is on the safe side
90+
print(f"update_ui_threadsafe_if_foreground called more than 100 times for one UI frame, which can overflow - throttling!")
91+
return None
8992
# lv.async_call() is needed to update the UI from another thread than the main one (as LVGL is not thread safe)
9093
result = lv.async_call(lambda _: self.if_foreground(func, *args, **kwargs),None)
9194
return result

0 commit comments

Comments
 (0)