(when) would it be wise to use asyncio ? #15105
-
Working (still) on my model railway automation, I have developed drivers / interfaces to control tracks, signals, junction, speed as well as playing wav_files to produce sounds to go with for example steamlocs. For that I greatfully build on the work of Mike Teachman and created a 4 channel, 2-Pico based "sounds" module using I2S. There I used asyncio which, given the file_Io involved, made a lot of sense. Now I try to develop a Controller to direct the movements of multiple trains and wonder if and why I should use asyncio here too. The ultimate architecture will be a Master (likely a rasp Pi), the main track_controller, the sound module (Pico's) and an external module (Pico) to interface to all sensors. So yes, there is some (UART) I/O but not that much and the UART fifo's will create a level of async behavior already. Apart from that I will be a good learning experience trying to develop it, what should be my overwhelming arguments to take an asyncio approach ? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
In my view any project involving concurrency is best handled with Most of the professional firmware projects I was involved with were based on cooperative multi-tasking - and those that were not had clear technical reasons for this design choice. |
Beta Was this translation helpful? Give feedback.
-
You mentioned file I/O is a good candidate for asyncio. I would also add network I/O. If you're servicing network requests, asyncio is the way to go. I've used asyncio for my MicroPython-based HTTP server and my MicroPython-based FTP server. In both cases, I've been very impressed with the responsiveness serving clients, particularly considering the minimal hardware (ESP32C and ESP8266) I've been running on. You mentioned using Raspberry Pi Pico. The Pico W has WiFi. You could benefit from asyncio if you were to add any network features to your project. |
Beta Was this translation helpful? Give feedback.
In my view any project involving concurrency is best handled with
asyncio
. Using interrupts, threads and timers is necessary in certain applications, usually when fast responses or accurate timing is required. If a response to an external event in (say) 100ms is acceptable,asyncio
usually leads to a better design.Most of the professional firmware projects I was involved with were based on cooperative multi-tasking - and those that were not had clear technical reasons for this design choice.