@opsys
Just a quick few tips on using the asycnio in the code snippet you posted, as I see some problems with your code.
You refer to a PinWatch class snippet I posted a long time back
. You do not show that class in your snippet but just show
class PinWatch():
pin_debounce_ms = 50
[...]
so I dont know exactly how much of the program code outside of the class definition you had in your program. Regardless, I suggest you structure your code in the following way if you are starting out with using asyncio:
Class definitions
other Functions
The function that runs when the asyncio loop is started (often called async def main(), but whatever name you like)
Finally start the asyncio loop.
Dont start the asyncio loop in a class definition (not that you can't but don't do this until you are familiar with programming in asyncio
And don't start the asynio loop within a while True loop.
The asyncio loop is started and it runs an async function. This function is the programs start point, but also remember that when its done all its bit of program code contained within this function, the async loop will end regardless of whether this function has kicked off other async tasks that are still running at that time. Thats why this function (often called main()) will often be ended with
while True:
await asyncio.sleep(0)
to keep that (main())function running, thus keeping the whole async programs running.
When you start the asyncio loop running, then any program code you put after that statement will only get to run once the asyncio loop has stopped.
When ending the program, perhaps with a Ctrl C, then to prevent some bits of the loop still being in existence, then its good practice to start the asynio loop in a try / except and have a finally statement - asyncio.new_event_loop() - that will clear up stuff from the existing loop, otherwise you may need to reboot the pico before running another program.
Consider if the methods in your class may be better just kept as separate functions as there does not appear to be any benefit from have this collection of functions grouped into a class, but you do not show your code that makes use of your class so thats just a quick comment.
My suggestion is that you bear the above points in mind and have a go at restructuring your program by taking the example I posted, adding your own class (without trying to start the async loop within your class and not within a while True loop) and to then both use your class and the PinWatch class from the program code that follows after the if __name__ == '__main__' in the example code I posted. That was put in so that the PinWatch class could be imported into another program, but if you are putting all your code into one file then you could remove that if __name__ ... which can be a pain as you then have to indent all your following code.
Have fun and I hope my remarks are useful to you.
Just a quick few tips on using the asycnio in the code snippet you posted, as I see some problems with your code.
You refer to a PinWatch class snippet I posted a long time back

class PinWatch():
pin_debounce_ms = 50
[...]
so I dont know exactly how much of the program code outside of the class definition you had in your program. Regardless, I suggest you structure your code in the following way if you are starting out with using asyncio:
Class definitions
other Functions
The function that runs when the asyncio loop is started (often called async def main(), but whatever name you like)
Finally start the asyncio loop.
Dont start the asyncio loop in a class definition (not that you can't but don't do this until you are familiar with programming in asyncio
And don't start the asynio loop within a while True loop.
The asyncio loop is started and it runs an async function. This function is the programs start point, but also remember that when its done all its bit of program code contained within this function, the async loop will end regardless of whether this function has kicked off other async tasks that are still running at that time. Thats why this function (often called main()) will often be ended with
while True:
await asyncio.sleep(0)
to keep that (main())function running, thus keeping the whole async programs running.
When you start the asyncio loop running, then any program code you put after that statement will only get to run once the asyncio loop has stopped.
When ending the program, perhaps with a Ctrl C, then to prevent some bits of the loop still being in existence, then its good practice to start the asynio loop in a try / except and have a finally statement - asyncio.new_event_loop() - that will clear up stuff from the existing loop, otherwise you may need to reboot the pico before running another program.
Consider if the methods in your class may be better just kept as separate functions as there does not appear to be any benefit from have this collection of functions grouped into a class, but you do not show your code that makes use of your class so thats just a quick comment.
My suggestion is that you bear the above points in mind and have a go at restructuring your program by taking the example I posted, adding your own class (without trying to start the async loop within your class and not within a while True loop) and to then both use your class and the PinWatch class from the program code that follows after the if __name__ == '__main__' in the example code I posted. That was put in so that the PinWatch class could be imported into another program, but if you are putting all your code into one file then you could remove that if __name__ ... which can be a pain as you then have to indent all your following code.
Have fun and I hope my remarks are useful to you.
Statistics: Posted by SirFico — Fri Dec 13, 2024 12:31 am