Perspective - Message handler & later.py GUI component assignments

We have an application that triggers the execution of a query which can run for several seconds, and during that time we are showing a loading indicator by greying out certain UI components and displaying a spinning loading icon.

This was working well with the later.py library. We would call the named query via scripting in an asynchronous function (using later.callAsync), and then set the "is loading" component back to false, along with several other GUI components, using later.assignLater().

Well, I found out that Message Handler scripts will execute in a separate asynchronous thread. If I moved my query call from the callAsync function to a Message Handler, should I still be using later.assignLater() for the assignment of GUI components? Or can I assign a value directly since the message is executing in a separate thread? I'm having trouble understanding the difference between something executed from the callAsync, and something executed from a Message Handler.

Thanks in advance

Wouldn't you be calling the query with: https://www.docs.inductiveautomation.com/docs/8.1/appendix/scripting-functions/system-util/system-util-invokeAsynchronous
Then from that async function sending a message to any listening message handlers in the session that sets the isLoading parameter to false? That way the query runs happily in the background and any script that depends on it just listens for the message it will send on completion, no extra configuration needed?

Perspective is a totally different threading environment from Vision. Use message handlers as needed instead of using later.assignLater().

1 Like

That is essentially what I was trying to do with the later.py library. It uses invokeAsynchronous to call my function (which runs the query) and then the library supplies a function which assigns the GUI component once the async function is complete. Though it sounds like I need to just use a message handler like you said instead of the library.

I see. I was under the impression that later.py would work with Perspective. I'll stick to the message handling. Does using system.perspective.sendMessage start in a new thread, similar to invokeAsynchronous? If so then could just run the query in the message handler, correct?

No, but Perspective is highly multi-threaded, so it effectively runs asynchronously.

later.py does include an emulation of invokeLater for use in gateway/perspective scope, but is intended for situations where its single thread is important for correctness.

You should avoid using invokeAsynchronous for Perspective UI tasks.