What is the alternate for sleep()?

Hi,

I found many threads in the forum for discussion on sleep(). Though I know this is not recommended to use, In my existing project there were several places sleep() has been used. So I want to get rid of this and looking for alternative. Which would be the best alternate approach?

I found that in the manual “If a sleep() or While Loop function must be used, then they should be called from system.util.invokeAsynchronous to prevent blocking any other scripts.” Since system.util.invokeLater is not supported in Perspective.
How to use this for my case?

We don't know what your case is, so it's hard to say.

1 Like

Unlike Vision, Perspective is fairly tolerant of short sleeps (not in a loop). We need to know how it is used by your application to know how to help you, if even necessary.

1 Like

Share the code that is using it, it would illuminate things for us.

We are using the scripts in UI on multiple event scripts and transform scripts like example

def runAction(self):
    import time
    self.session.custom.Anim = False
    self.getChild("root").getChild("Icon").props.style.transform = 'rotate(0deg)'
    time.sleep(1)

Similar usage in multiple places.. so I want to look for alternate if really causing the performance issue

the time also varies from 1 s to 10s

huh ? Why ?

Wait, are you using sleep to set an animation time ?
Like, you're resetting .custom.Anim to false, and the transform to null after the sleep ?

You probably want to use CSS for this. What exactly are you trying to do?

If I’m understanding correctly, it looks like you’re running code to reset the rotation on something every second. that would definitely cause performance issues to do it that way.

I try to find another way, but sometimes it's much easier to just use a Timer:

from threading import Timer

def doSomethingLater():
		# do it...

Timer(0.2, doSomethingLater).start()
1 Like

Hi,

To clarify the use case, actually we are showing a load Icon while changing the view in the Display.

def runAction(self):
Import time
if self.session.custom.Anim == True:
        while self.session.custom.Anim == True:
            for degree in range(0, 2000, 10):
                self.getChild("root").getChild("Icon").props.style.transform = 'rotate(' + str(degree) + 'deg)'
                time.sleep(0.25)
    
                if self.session.custom.Anim == False:
                    break  
            
            if self.session.custom.Anim == False:
                break  

On each view change we are enabling and disabling the custom property. This has been implemented previously. So just want to understand this will cause any performance issue? Shall we use system.util.invokeAsynchronous instead time.sleep? In thread Dashboard we are seeing multiple process is in state of Timed Waiting and Waiting. So we need to understand what is causing the below status…

I see you've made another post related to trying to determine what all the threads are doing. Are you experiencing a log of performance problems or something? Are these related?

That is an unbounded loop. You should never use those in Ignition event scripts, whether using .sleep() or not. Event scripts should run to completion in a repeatable duration, preferably in a handful of milliseconds.

Any longer delays should be handled by reacting to property changes as data flows in your UI, or by including polling bindings with associated scripts, perhaps in gateway timer events.

If you have a process that is heavily sequential, and you can prevent repeat launches while a prior is running, then invokeAsynchronous might be appropriate.

3 Likes