_thread.start_new_thread: How to use kwargs #17584
-
Baffled by My function to spawn as a thread has this signature: Allright, so I remove those args to get this signature: Other attempts: MicroPython v1.25.0 on 2025-04-15; Generic ESP32 module with ESP32 |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
I don't know the details of your script, but maybe this will help: import _thread
import time
class test():
def serverReport(self, id, what):
print(f'ID: {id[0]}/{id[1]}, CMD: {what['cmd']} {what['val']}')
t = test()
# t.serverReport(('PRG', 123), {'cmd':'add', 'val':(3,4)})
# new_thread(func, (param))
_thread.start_new_thread(t.serverReport, (('PRG', 123), {'cmd':'add', 'val':(3,4)}))
time.sleep(5) # wait a bit
print('Done!') This will give:
|
Beta Was this translation helpful? Give feedback.
-
The args must be presented to import _thread
import time
class test():
def serverReport(self, id, num, cmd, val): # two positional args and two kwargs
print(f'ID: {id}, num {num}, cmd {cmd}, val {val}')
t = test()
_thread.start_new_thread(t.serverReport, ('PRG', 123), {'cmd':'add', 'val':(3,4)})
time.sleep(5) # wait a bit
print('Done!') prints: ID: PRG, num 123, cmd add, val (3, 4) |
Beta Was this translation helpful? Give feedback.
I don't know the details of your script, but maybe this will help:
This will give: