Skip to content

Commit 43eb822

Browse files
Move mpos.apps.good_stack_size() to TaskManager.good_stack_size()
Trying to get every app-facing API as part of an object.
1 parent 7b65ec7 commit 43eb822

File tree

9 files changed

+30
-58
lines changed

9 files changed

+30
-58
lines changed

internal_filesystem/builtin/apps/com.micropythonos.wifi/assets/wifi.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
import lvgl as lv
33
import _thread
44

5-
from mpos import Activity, Intent, MposKeyboard, WifiService, CameraActivity, DisplayMetrics, CameraManager
6-
import mpos.apps
5+
from mpos import Activity, Intent, MposKeyboard, WifiService, CameraActivity, DisplayMetrics, CameraManager, TaskManager
76

87
class WiFi(Activity):
98
"""
@@ -101,7 +100,7 @@ def start_scan_networks(self):
101100
self.busy_scanning = True
102101
self.scan_button.add_state(lv.STATE.DISABLED)
103102
self.scan_button_label.set_text(self.scan_button_scanning_text)
104-
_thread.stack_size(mpos.apps.good_stack_size())
103+
_thread.stack_size(TaskManager.good_stack_size())
105104
_thread.start_new_thread(self.scan_networks_thread, ())
106105

107106
def refresh_list(self):
@@ -179,7 +178,7 @@ def start_attempt_connecting(self, ssid, password):
179178
print("Not attempting connect because busy_connecting.")
180179
else:
181180
self.busy_connecting = True
182-
_thread.stack_size(mpos.apps.good_stack_size())
181+
_thread.stack_size(TaskManager.good_stack_size())
183182
_thread.start_new_thread(self.attempt_connecting_thread, (ssid, password))
184183

185184
def attempt_connecting_thread(self, ssid, password):

internal_filesystem/lib/mpos/apps.py

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,6 @@
66
import mpos.info
77
import mpos.ui
88

9-
def good_stack_size():
10-
stacksize = 24*1024 # less than 20KB crashes on desktop when doing heavy apps, like LightningPiggy's Wallet connections
11-
import sys
12-
if sys.platform == "esp32":
13-
stacksize = 16*1024
14-
return stacksize
15-
169
# Run the script in the current thread:
1710
# Returns True if successful
1811
def execute_script(script_source, is_file, classname, cwd=None):
@@ -81,34 +74,6 @@ def execute_script(script_source, is_file, classname, cwd=None):
8174
traceback.print_exception(type(e), e, tb)
8275
return False
8376

84-
""" Unused:
85-
# Run the script in a new thread:
86-
# NOTE: check if the script exists here instead of launching a new thread?
87-
def execute_script_new_thread(scriptname, is_file):
88-
print(f"main.py: execute_script_new_thread({scriptname},{is_file})")
89-
try:
90-
# 168KB maximum at startup but 136KB after loading display, drivers, LVGL gui etc so let's go for 128KB for now, still a lot...
91-
# But then no additional threads can be created. A stacksize of 32KB allows for 4 threads, so 3 in the app itself, which might be tight.
92-
# 16KB allows for 10 threads in the apps, but seems too tight for urequests on unix (desktop) targets
93-
# 32KB seems better for the camera, but it forced me to lower other app threads from 16 to 12KB
94-
#_thread.stack_size(24576) # causes camera issue...
95-
# NOTE: This doesn't do anything if apps are started in the same thread!
96-
if "camtest" in scriptname:
97-
print("Starting camtest with extra stack size!")
98-
stack=32*1024
99-
elif "appstore" in scriptname:
100-
print("Starting appstore with extra stack size!")
101-
stack=24*1024 # this doesn't do anything because it's all started in the same thread
102-
else:
103-
stack=16*1024 # 16KB doesn't seem to be enough for the AppStore app on desktop
104-
stack = mpos.apps.good_stack_size()
105-
print(f"app.py: setting stack size for script to {stack}")
106-
_thread.stack_size(stack)
107-
_thread.start_new_thread(execute_script, (scriptname, is_file))
108-
except Exception as e:
109-
print("main.py: execute_script_new_thread(): error starting new thread thread: ", e)
110-
"""
111-
11277
# Returns True if successful
11378
def start_app(fullname):
11479
from .content.package_manager import PackageManager

internal_filesystem/lib/mpos/audio/audioflinger.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# Uses _thread for non-blocking background playback/recording (separate thread from UI)
77

88
import _thread
9-
import mpos.apps
9+
from ..task_manager import TaskManager
1010

1111

1212
class AudioFlinger:
@@ -164,7 +164,7 @@ def play_wav(self, file_path, stream_type=None, volume=None, on_complete=None):
164164
on_complete=on_complete
165165
)
166166

167-
_thread.stack_size(mpos.apps.good_stack_size())
167+
_thread.stack_size(TaskManager.good_stack_size())
168168
_thread.start_new_thread(self._playback_thread, (stream,))
169169
return True
170170

@@ -208,7 +208,7 @@ def play_rtttl(self, rtttl_string, stream_type=None, volume=None, on_complete=No
208208
on_complete=on_complete
209209
)
210210

211-
_thread.stack_size(mpos.apps.good_stack_size())
211+
_thread.stack_size(TaskManager.good_stack_size())
212212
_thread.start_new_thread(self._playback_thread, (stream,))
213213
return True
214214

@@ -285,7 +285,7 @@ def record_wav(self, file_path, duration_ms=None, on_complete=None, sample_rate=
285285
)
286286

287287
print("AudioFlinger: Starting recording thread...")
288-
_thread.stack_size(mpos.apps.good_stack_size())
288+
_thread.stack_size(TaskManager.good_stack_size())
289289
_thread.start_new_thread(self._recording_thread, (stream,))
290290
print("AudioFlinger: Recording thread started successfully")
291291
return True

internal_filesystem/lib/mpos/board/fri3d_2024.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import mpos.ui
1717
import mpos.ui.focus_direction
1818

19+
from ..task_manager import TaskManager
1920

2021
# Pin configuration
2122
SPI_BUS = 2
@@ -386,7 +387,7 @@ def startup_wow_effect():
386387
except Exception as e:
387388
print(f"Startup effect error: {e}")
388389

389-
_thread.stack_size(mpos.apps.good_stack_size()) # default stack size won't work, crashes!
390+
_thread.stack_size(TaskManager.good_stack_size()) # default stack size won't work, crashes!
390391
_thread.start_new_thread(startup_wow_effect, ())
391392

392393
print("fri3d_2024.py finished")

internal_filesystem/lib/mpos/main.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
from .content.package_manager import PackageManager
1010
from .ui.appearance_manager import AppearanceManager
1111
from .ui.display_metrics import DisplayMetrics
12-
import mpos.ui.topmenu
1312

13+
import mpos.ui.topmenu
14+
from .task_manager import TaskManager
1415

1516

1617
# White text on black logo works (for dark mode) and can be inverted (for light mode)
@@ -124,7 +125,7 @@ def custom_exception_handler(e):
124125

125126
try:
126127
from mpos.net.wifi_service import WifiService
127-
_thread.stack_size(mpos.apps.good_stack_size())
128+
_thread.stack_size(TaskManager.good_stack_size())
128129
_thread.start_new_thread(WifiService.auto_connect, ())
129130
except Exception as e:
130131
print(f"Couldn't start WifiService.auto_connect thread because: {e}")

internal_filesystem/lib/mpos/task_manager.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@ def start(cls):
2626
print("Not starting TaskManager because it's been disabled.")
2727
return
2828
cls.keep_running = True
29-
# New thread works but LVGL isn't threadsafe so it's preferred to do this in the same thread:
30-
#_thread.stack_size(mpos.apps.good_stack_size())
31-
#_thread.start_new_thread(asyncio.run, (self._asyncio_thread(100), ))
32-
# Same thread works, although it blocks the real REPL, but aiorepl works:
3329
asyncio.run(TaskManager._asyncio_thread(10)) # 100ms is too high, causes lag. 10ms is fine. not sure if 1ms would be better...
3430

3531
@classmethod
@@ -70,3 +66,12 @@ def notify_event():
7066
@staticmethod
7167
def wait_for(awaitable, timeout):
7268
return asyncio.wait_for(awaitable, timeout)
69+
70+
@staticmethod
71+
def good_stack_size():
72+
stacksize = 24*1024 # less than 20KB crashes on desktop when doing heavy apps, like LightningPiggy's Wallet connections
73+
import sys
74+
if sys.platform == "esp32":
75+
stacksize = 16*1024
76+
return stacksize
77+

internal_filesystem/lib/threading.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
# Lightweight replacement for CPython's Thread module
2+
13
import _thread
24

5+
from .task_manager import TaskManager
36
import mpos.apps
47

58
class Thread:
@@ -21,7 +24,7 @@ def start(self):
2124
# small stack sizes 8KB gives segfault directly
2225
# 22KB or less is too tight on desktop, 23KB and more is fine
2326
#stacksize = 24*1024
24-
stacksize = mpos.apps.good_stack_size()
27+
stacksize = TaskManager.good_stack_size()
2528
#stacksize = 20*1024
2629
print(f"starting thread with stacksize {stacksize}")
2730
_thread.stack_size(stacksize)

tests/test_multi_connect.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22
import _thread
33
import time
44

5-
from mpos import App, PackageManager
6-
import mpos.apps
5+
from mpos import App, PackageManager, TaskManager
76

87
from websocket import WebSocketApp
98

10-
119
# demo_multiple_ws.py
1210
import asyncio
1311
import aiohttp
@@ -137,7 +135,7 @@ def newthread(self):
137135
asyncio.run(self.main())
138136

139137
def test_it(self):
140-
_thread.stack_size(mpos.apps.good_stack_size())
138+
_thread.stack_size(TaskManager.good_stack_size())
141139
_thread.start_new_thread(self.newthread, ())
142140
time.sleep(10)
143141

@@ -253,6 +251,6 @@ def newthread(self, url):
253251

254252
def test_it(self):
255253
for url in self.WS_URLS:
256-
_thread.stack_size(mpos.apps.good_stack_size())
254+
_thread.stack_size(TaskManager.good_stack_size())
257255
_thread.start_new_thread(self.newthread, (url,))
258256
time.sleep(15)

tests/test_multi_websocket_with_bad_ones.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import time
44

55
from mpos import App, PackageManager
6-
import mpos.apps
6+
from mpos import TaskManager
77

88
from websocket import WebSocketApp
99

@@ -136,7 +136,7 @@ def newthread(self):
136136
asyncio.run(self.main())
137137

138138
def test_it(self):
139-
_thread.stack_size(mpos.apps.good_stack_size())
139+
_thread.stack_size(TaskManager.good_stack_size())
140140
_thread.start_new_thread(self.newthread, ())
141141
time.sleep(10)
142142

0 commit comments

Comments
 (0)