Page 2 of 2

Re: Traffic lights with a button interrupt - how would you have done it?

Posted: Sat Jul 23, 2022 5:20 pm
by karfas
@pythoncoder: You misunderstood me, I think. Even when this came out as an anti-asyncio rant, I also wrote
karfas wrote:
Fri Jul 22, 2022 3:55 pm
asyncio is great for doing many different things in parallel, and it's definitely worth learning.
Handling 300 traffic lights in a small city ? Asyncio, of course. Nothing better exists in the moment.
asyncio in an example for a newbie ? No way.

Despite my experience with different async/cooperative/multitasking environments during the last 45 years, I still struggle to wrap my head around some of the (u)asyncio concepts.

Do you have one or two pointers to one of these large asyncio programs so I could study how things work here ? I mean real programs, not some library ?

Re: Traffic lights with a button interrupt - how would you have done it?

Posted: Sun Jul 24, 2022 10:58 am
by pythoncoder
To give a simple example of where uasyncio trumps the alternative, consider a requirement to flash an LED at a rate which can be varied. Then extend it to say four LED's, all flashing at different rates and with each rate subject to variability. Try to code that using a state machine or otherwise and it soon becomes an evil tangle. In uasyncio:

Code: Select all

class FlashingLed:
    def __init__(pin, period):
        self.period = period
        self.pin = pin
        asyncio.create_task(self.run())

    async def run(self):
        while True:
            await asyncio.sleep_ms(self.period)
            self.pin(not self.pin())
Instantiate as many of those as you wish, change their periods at will. Simple logic, no globals, good OOP practice, and minimal hardware dependence.

As for examples, you could look at micro-gui or asynchronous MQTT. Admittedly these are libraries (what have you got against them?) but there is a music player here which uses micro-gui.

Re: Traffic lights with a button interrupt - how would you have done it?

Posted: Wed Jul 27, 2022 4:08 am
by curt
The traffic light application is interesting. I developed an example application for a module I am developing. It can be found here:
https://github.com/ctimmer/poll-looper

You only need:
poll_looper.py
and
example/trafficlights.py (app)

It was written for micropython but it runs on my raspberry pi (python3). The application only prints so no IO pins are used.

Curt