I know you shouldn't use time.sleep(), but when you can't use run_in, what to do?

I’m not sure how seriously I should take the admonition that you shouldn’t use time.sleep(). Are there exceptions? In my example I have a loop that has to determine when a train of signals stop repeating. When that time period exceeds 0.3 seconds I register that and create a new event. run_in is not that exact for sub-second time periods (I tried) so is it a big deal if I have a loop with a time.sleep(0.1) in it?

What are the alternatives, do I need to find an alternative?

appdaemon uses threads.
and as long as a callback is busy the thread is active and unavailable for other apps.

thats why normally a callback should be the fastest way to get in and out.

you can use threading to create threads that are seperate from appdaemon. for looping thats the best way.
in that case there is also no problem to use sleep.

the alternative is to raise the amount of threads for appdaemon, but in 3.0.2 thats masking bad coding, and can cause trouble when you got lots of stuff going on.

so now you know why its illadvised to use time.sleep, its up to you to decide how to use it.

in the coming AD 4.0 you will be able to use shorter times then 1 second.
and threading will be different.

Thank you for your answer. Am I understanding you correctly? Me using threading wouldn’t create any performance benefits unless I’m risking to run out of threads in AD?

I’m not particular worried about running out of threads because of this particular app, so if there are no performance penalties besides that, I’ll wait for AD 4.0 before looking for another implementation.

AD uses the amount of threads that is set in appdaemon.yaml (default 10 if i am correct) for all apps.
if 1 thread is busy, the next will be used.

if you use the threading module, you create a thread outside the AD threadpool, so AD wont suffer.
thats why that is better used for loops.

normally AD wont suffer performance from a busy thread, unless you get over the set amount for the threadpool.

off course your CPU will be way more busy, because you got an active thread running all the time.
so you need to look if that will cause trouble or not.