How to join threads?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
User avatar
aivarannamaa
Posts: 123
Joined: Fri Sep 22, 2017 3:19 pm
Location: Estonia
Contact:

How to join threads?

Post by aivarannamaa » Mon Feb 01, 2021 2:38 pm

Is there a built-in support for waiting a thread to complete in MicroPython (eg. on ESP32 or RPi Pico)?

If not, then can you recommend a technique for achieving this? Something less ugly than sleeping in a loop and checking a shared flag?

My goal is to recommend a work-around for Thonny users, who get errors when they are using threads (eg. https://github.com/thonny/thonny/issues/1586). When Thonny detects REPL prompt, it thinks the device is idle and it can issue hidden commands (eg. for listing files) without being disturbed. Printing from a thread invalidates this expectation.

I know Thonny should try harder and distinguish the output meant for user from the output from management commands, but for a work-around it would be good if it was possible to organize the user code such that the main thread doesn't return to the prompt before all background threads are done.
Aivar Annamaa
https://thonny.org

User avatar
jimmo
Posts: 2262
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: How to join threads?

Post by jimmo » Tue Feb 02, 2021 4:15 am

aivarannamaa wrote:
Mon Feb 01, 2021 2:38 pm
Is there a built-in support for waiting a thread to complete in MicroPython (eg. on ESP32 or RPi Pico)?
Unfortunately not that I'm aware of.
aivarannamaa wrote:
Mon Feb 01, 2021 2:38 pm
If not, then can you recommend a technique for achieving this? Something less ugly than sleeping in a loop and checking a shared flag?
That's pretty much how join works under the hood but yeah probably a neater solution than using a flag directly is to use a mutex held by the thread and the main thread shouldn't exit until it can acquire the mutex.

tonygo
Posts: 7
Joined: Thu Apr 12, 2018 1:23 pm

Re: How to join threads?

Post by tonygo » Fri Feb 19, 2021 3:20 pm

There is some discussion on The Ras Pi Pico Forum but it was a bit above my current experience:
https://www.raspberrypi.org/forums/view ... &p=1808139

Post Reply