Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 4876

MicroPython • How to exit gracefully from the second Pico thread ?

$
0
0
Hi !
I made the following code in MicroPython to learn how threading works in Raspberry Pico.
When I press Ctrl+C, it indeed exits, but I receive an error in terminal window:
PROBLEM IN THONNY'S BACK-END: Exception while handling 'Run' (thonny.plugins.micropython.mp_back.ManagementError: Command output was not empty).
See Thonny's backend.log for more info.
You may need to press "Stop/Restart" or hard-reset your MicroPython device and try again.
Process ended with exit code 1.
This type of error is triggered when the main thread finishes but the second is still running. First time I forgot to exit the 'while' loop and then I could no longer use the Pico with Thonny until I nuked the Pico flash and write the firmware again. Now it exits, but it still throw that error. And I even inserted a 1s delay after I signaled the thread to terminate... In Pico we don't have a way to wait for a thread to finish (something like thread.join()) ?
I also don't get the message "New thread is terminating gracefully."

Code:

import _threadimport timeterminate = Falsedef my_thread(EndFlag):  print("New thread is running...")  while not EndFlag:    time.sleep(0.2)   print("New thread is terminating gracefully.")    _thread.start_new_thread(my_thread, (terminate,))try:  while True:    time.sleep(2) except KeyboardInterrupt:  terminate = True  time.sleep(1)  print("Main thread terminated gracefully.")

Statistics: Posted by Marus780 — Thu Feb 29, 2024 8:57 pm — Replies 0 — Views 27



Viewing all articles
Browse latest Browse all 4876

Trending Articles