Create and use Class under asyncio context? But time.sleep() used?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
Jibun no kage
Posts: 144
Joined: Mon Jul 25, 2022 9:45 pm

Create and use Class under asyncio context? But time.sleep() used?

Post by Jibun no kage » Wed Aug 03, 2022 3:37 pm

Create and use Class under asyncio context? But time.sleep() used?

This maybe a general python question not unique to MicroPython, but since I am using said class on ESP and Pico, thought I would post it here. I have created a class that I plan or desire the flexibility to instance it both in an asyncio context and otherwise. I am using time.sleep() to delay the class execution of a specific method, and this is thread blocking for the given thread its is running on, ok, understood.

The question is, if this same class is in asyncio context, is time.sleep going to work, technically asyncio.sleep should be called no? To make sure the given asyncio event loop is serviced, right?

Is there a way to make a given class not context specific, i.e. under asyncio or not when used? Or do I really have to create two classes, one designed to work under asyncio and other that will be used outside of asyncio context?

User avatar
karfas
Posts: 193
Joined: Sat Jan 16, 2021 12:53 pm
Location: Vienna, Austria

Re: Create and use Class under asyncio context? But time.sleep() used?

Post by karfas » Wed Aug 03, 2022 5:15 pm

As far as I understood asyncio, classes have nothing to do with it.
Only functions and methods may get the (viral) async decorator.

What exactly is your use case for an async class and the sleep() ?

For myself, I concluded that actually doing something in __init__() beside setting some instance variables is a disadvantage in the long run. So I usually create one class and have a few async methods that I can use when I need to use the thing in async context.
Last edited by karfas on Wed Aug 03, 2022 5:18 pm, edited 1 time in total.
A few hours of debugging might save you from minutes of reading the documentation! :D
My repositories: https://github.com/karfas

DeaD_EyE
Posts: 19
Joined: Sun Jul 17, 2022 12:57 pm
Contact:

Re: Create and use Class under asyncio context? But time.sleep() used?

Post by DeaD_EyE » Wed Aug 03, 2022 5:17 pm

The question is, if this same class is in asyncio context, is time.sleep going to work, technically asyncio.sleep should be called no? To make sure the given asyncio event loop is serviced, right?
time.sleep blocks the main thread. The event loop is blocked during time.sleep().

Is there a way to make a given class not context specific, i.e. under asyncio or not when used? Or do I really have to create two classes, one designed to work under asyncio and other that will be used outside of asyncio context?
  • extend your class with async methods.
  • factor out common methods into a base class, which don't use blocking synchronous calls like time.sleep(). Then make two classes which inherit from the base class and add the methods. The methods on the synchronous implementation could have the same names, like the asynchronous implementation. The only difference is, that you use in the asynchronous implementation async methods and inside await for coroutines.
  • the use of threads is not a good option, because of the limitation across different ports. For example, the rp2 only supports two threads. The main thread and one additional thread. With the esp32 you can make ~10 threads (depending on free memory).

Jibun no kage
Posts: 144
Joined: Mon Jul 25, 2022 9:45 pm

Re: Create and use Class under asyncio context? But time.sleep() used?

Post by Jibun no kage » Wed Aug 03, 2022 9:59 pm

Interesting because in C# for example you would just override/overload the class method, and so depending on now you invoke the method, would determine how it was handled.

Creating two classes, off a common base, is pretty much the methodology I was looking at. In my specific use case, I am using a Pico, so I wanted to push update, i.e. change of state of some diodes to the other processor.

For now have the entire diode control class running on the other processor code and it is 100% under uasyncio context. I had to restructure the class a bit but works.

Post Reply