Page 1 of 1

How to join threads?

Posted: Mon Feb 01, 2021 2:38 pm
by aivarannamaa
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.

Re: How to join threads?

Posted: Tue Feb 02, 2021 4:15 am
by jimmo
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.

Re: How to join threads?

Posted: Fri Feb 19, 2021 3:20 pm
by tonygo
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