PDA

View Full Version : Threaded JS/script?



bibbinator
22nd June 2015, 17:12
Hi,
I want threaded execution of long-running scripts and am unsure the best way to do this in Qt.

In my app I want to allow users to write scripts in javascript that modify data in my app. Each script can show some settings which are defined in QML. Multiple scripts can be applied to the same data.

The scripts are invoked whenever the data is modified and is considered a long-running process. Since there are potentially many such scripts being applied to a data set, I would like to have the scripts be threaded.

I saw this which outlines the options: http://doc.qt.io/qt-5/threads-technologies.html

I see I can use a WorkerScript to run the user's script from QML but:

* It looks like I can't have the script post progress back to the QML UI such as a progress bar?
* No way to cancel or restart in the event the user changes the settings while the WorkerScript is running?
* No way to have the source be anything other than a standalone file? [it's messy to require multiple files per data filter and was hoping for a single file solution]

I see QtThread but:

* Is there a way to invoke a JS method from a thread and get updates/progress/cancel, etc.?
* This wants a C++ method but I want to use a script, so it means I need to create a new context within the thread and this seems really bad/inefficient?

I see QtConcurrent but:

* Is there a way to invoke a JS method from within this and still get updates/progress/cancel, etc.?
* This wants a C++ method but I want to use a script, so it means I need to create a new context within the thread and this seems really bad/inefficient?

Or is there a better way to run a script that's callable, cancelable, reports progress and can be multi-threaded?

All help appreciated!
Brett

anda_skoa
22nd June 2015, 21:56
You could create a class that is registered with QML and can take whatever arguments you need for a script.
The class would then use QThread internally and a new instance of a script engine within that thread's context (either created by the thread or moved to it).

I am pretty sure that WorkerScript does something along those lines.

Cheers,
_

bibbinator
23rd June 2015, 06:41
Okay, thanks. If I can move the script engine into the thread then this sounds like a good approach.

anda_skoa
23rd June 2015, 09:03
Okay, thanks. If I can move the script engine into the thread then this sounds like a good approach.

I don't see why this would not be possible unless the script engine has some kind of unprotected shared state between instances (which I highly doubt).
But you can always check the implementation of WorkerScript :)

Cheers,
_