As system.util.invokeLater() doesn’t work in perspective, what is the alternative if I want to add some delay before executing my code in perspective script environment?
for example after setting my flag property in session property, I want it automatically clear after 5 sec.
I can do it in change script event of the property with sleep() to clear it but it may block hole thread.
Do you want exactly five seconds to have passed? I usually just have a Gateway Timer Script send out a message every x seconds, and then I have my Session listen for that “ping”, then take an action. This results in me toggling the flag anywhere within the specified delay in the Timer Script though.
You should configure a Gateway Timer Script (
Project > Gateway Events > Timer) with the followng script:
# I have a project named "Base", and that project has a configured "Message" handler named "TOGGLE_FLAG". # Note that the scope value does not match the scope values you would normally use in Perspective. system.util.sendMessage(project='Base', messageHandler='TOGGLE_FLAG', scope='S')
Now configure your message handler (
Project > Gateway Events > Timer). This message handler is used by the Session itself to hear messages from the Gateway or even other Sessions.
# Note here that we are using the perspective package instead of util system.perspective.sendMessage('SESSION_TOGGLE_FLAG', scope='session')
Now you just need to configure something in the session with a message handler to complete the process by actually toggling the flag.
The problem is if I set my flag in the middle of 5 sec timer(for example 3sec) it will trigger to reset in less than 5 sec.(2 sec).
I try to use change script on my session property and in there use python timer, but it is not working as intended always.
For example if the property write between the delay timer, the python timer should create several thread and reset my flag. It happen but after the finishing all thread for the next flag change the timer doesn’t work and reset property immediately.
May be using python timer in change value script for session property has some kind of bug.
I use following solutions and it work totally fine but it is a little messy.
I some how create timer by binding now(1000) in a property. and in each change it trigger a script for handling the rest.
The only cons here is it execute every sec instead of event base.
Using an expression binding with now() is a good approach. Consider passing an integer custom property as the argument for now(), that you can set to 0 when your state machine is idle. Set it back to 1000 as part of your start action.
I’ve been experimenting with having to deal with not having the invokeLater function within perspective’s scope. The now() expression can work, but if you are working with something that doesn’t allow for the use of the expression language (gateway scripts for example), this isn’t an option. And the first solution the @cmallonee offered does have some issues which nader pointed out in which if his trigger starts at 2.5 seconds of the gateway’s 5-second timer, well then, its not a 5 second delay.
I always get pretty nervous using while loops since if you aren’t careful it is pretty easy to create an infinity loop. One bit of code I’ve been working with is to have a failsafe so if the while loop runs for more than however many seconds, then it will break. This script is waiting for values to go back to zero before the rest of the script carries on/ Here’s the code:
then = system.date.now() while True: demand = system.tag.readBlocking([tag1]).value scanning = system.tag.readBlocking([tag2]).value if demand == False and scanning == 0: break sec_between = system.date.secondsBetween(then, system.date.now()) if sec_between > 200: break ##Resume rest of scrip after vals return to 0 and False
I’d be interested in hearing any other approaches to scripting that allows for a delay for things to happen in a script before the rest of the script carries on.
Unfortunately none the system.util functions can be used in perspective as all scripting of a session 8s done in a single thread at server side.
Trigging a script with now is only option but it also can unnecessary overload.
This is some thing that only IA devs can answer.
Search this forum for “later.py”. (: