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

MicroPython • Re: Multithreads problems.

$
0
0
Here's a thought ...

Let your main "while (1)" loop do its thing and have your external user events be handled by an interrupt servicing strategy.

Slow asynchronous events like user keyboard inputs are better serviced by an interrupt that echoes and buffers input until a whole line/command is built and then exec it on "Enter" then effecting changes to global variables to be accessed in the main "while (1)" loop or handle single events and deal with them again changing global variables.

You might need to include global semaphores to synchronize and indicate new input but that's relatively easy to include.

Its sometimes worth having your main loop wait for a regular "Tick Event" from a timer interrupt rather than whizzing too fast unless you want to complete a years worth of day/night cycles before tea time.

All this can be done single threaded leaving the other core to Micropython and its background tasks.

Note: by "global variables" I mean a variable accessible from everywhere in your program as distinct from local variable being private to the current function/def.

Statistics: Posted by RaspISteve — Mon Jul 08, 2024 4:10 pm



Viewing all articles
Browse latest Browse all 4151

Trending Articles