MP _thread graceful exit? #17959
Closed
Jibun-no-Kage
started this conversation in
General
Replies: 2 comments
-
Use threads with locks. Makes life much easier. In my example, the very simple test mechanism can run about 160 thousand times per second. Intersperse your thread code with those tests (and returns) at the appropriate locations. #!micropython
# -*- coding: UTF-8 -*-
# vim:fileencoding=UTF-8:ts=4
import _thread
from time import ticks_us, ticks_diff, sleep_ms
from array import array
_THREAD_RUN = const(0)
_THREAD_FIN = const(1)
def oncore1_a(run, data):
while run.locked():
data[1] += 1
data[0] = _THREAD_FIN
def oncore1_b(run, data):
while run.locked():
data[2] += 1
data[0] = _THREAD_FIN
def oncore1_c(data):
data[1] = 0
data[2] = 0
data[0] = _THREAD_FIN
_thread.exit() # silent exit
def oncore1_d(run, data):
try:
while True: #
while True: # just to illustrate exit from deep within a nested loop
while True: #
data[1] += 1
if not run.locked(): _thread.exit()
except SystemExit: # happens only with _thread.exit()
data[2] = 444
finally: # happens always
data[2] <<= 1 # perform final cleanup
data[0] = _THREAD_FIN
def oncore0():
def prep_start(func, args):
run.acquire() # make sure thread can run
data[0] = _THREAD_RUN
t = ticks_us()
_thread.start_new_thread(func, args)
return t
def stop_wait_finished(t_ms):
sleep_ms(t_ms)
if run.locked():
run.release() # will core1 thread allow to end
while data[0] == _THREAD_RUN: # wait for finished flag
pass
return ticks_us()
data = array('I', (0,0,0)) # [0]running:0 finished:1 [1:2] counters
run = _thread.allocate_lock()
t0 = prep_start(oncore1_a,(run, data))
t1 = stop_wait_finished(100)
print(f'\ncount: {data}')
print(f'time: {ticks_diff(t1, t0)} µs')
t0 = prep_start(oncore1_b,(run, data))
t1 = stop_wait_finished(100)
print(f'\ncount: {data}')
print(f'time: {ticks_diff(t1, t0)} µs')
t0 = prep_start(oncore1_c,(data,))
t1 = stop_wait_finished(100)
print(f'\ncount: {data}')
print(f'time: {ticks_diff(t1, t0)} µs')
t0 = prep_start(oncore1_d,(run, data))
t1 = stop_wait_finished(100)
print(f'\ncount: {data}')
print(f'time: {ticks_diff(t1, t0)} µs')
oncore0() |
Beta Was this translation helpful? Give feedback.
0 replies
-
Excellent. Thank you. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
MP _thread graceful exit? I see _thread has an exit() method but that apparently generates SystemExit exception, that abort execution? I am using a loop variable I am setting so the thread task loop is 'told' to stop, but that passive... I believe I basically have a to have a static loop waiting for the thread loop to stop. That is kinda of an ugly hack... if the thread loop ends in 2 seconds, but the static wait has to be much longer in case the thread loop takes longer? Bit of a chick and egg or whatever thing. Full python you could do a join with timeout set, so the join completes as soon as the thread loop completes or times out... but for MP this might be a bridge too far given design goals of MP?
Beta Was this translation helpful? Give feedback.
All reactions