Skip to content

Commit 9ff5dfa

Browse files
add Daemon option to threading.py
1 parent 6c2793e commit 9ff5dfa

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

internal_filesystem/lib/threading.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,37 @@
11
import _thread
22

33
class Thread:
4-
def __init__(self, group=None, target=None, name=None, args=(), kwargs=None):
4+
def __init__(self, group=None, target=None, name=None, args=(), kwargs=None, daemon=None):
55
self.target = target
66
self.args = args
77
self.kwargs = {} if kwargs is None else kwargs
8+
self.name = name
9+
self.daemon = daemon # Store daemon attribute (True, False, or None)
810

911
def start(self):
12+
# In MicroPython, _thread.start_new_thread doesn't support daemon threads directly
13+
# We store the daemon attribute for compatibility, but it may not affect termination
1014
_thread.start_new_thread(self.run, ())
1115

1216
def run(self):
13-
self.target(*self.args, **self.kwargs)
17+
try:
18+
self.target(*self.args, **self.kwargs)
19+
except Exception as e:
20+
# Basic error handling to prevent silent failures
21+
print(f"Thread {self.name or ''} failed: {e}")
22+
23+
@property
24+
def daemon(self):
25+
return self._daemon
26+
27+
@daemon.setter
28+
def daemon(self, value):
29+
self._daemon = value if value is not None else False
1430

1531

1632
class Lock:
1733
def __init__(self):
18-
self._lock = _thread.allocate_lock()
34+
self._lock = _thread.allocate_lock()
1935

2036
def __enter__(self):
2137
if self._lock:

0 commit comments

Comments
 (0)