Results 1 to 4 of 4

Thread: Threaded JS/script?

  1. #1
    Join Date
    Oct 2009
    Posts
    27
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Threaded JS/script?

    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
    Last edited by bibbinator; 22nd June 2015 at 18:00.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Threaded JS/script?

    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,
    _

  3. #3
    Join Date
    Oct 2009
    Posts
    27
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Threaded JS/script?

    Okay, thanks. If I can move the script engine into the thread then this sounds like a good approach.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Threaded JS/script?

    Quote Originally Posted by bibbinator View Post
    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,
    _

Similar Threads

  1. Qt script combined with java script
    By prachi kamble in forum Newbie
    Replies: 8
    Last Post: 16th April 2015, 13:30
  2. Qt & External Threaded API
    By mreyfout in forum Qt Programming
    Replies: 0
    Last Post: 28th October 2010, 22:39
  3. Can I include a script from script?
    By yycking in forum Qt Programming
    Replies: 1
    Last Post: 24th April 2009, 03:01
  4. Threaded TCP server
    By Sysace in forum Newbie
    Replies: 4
    Last Post: 21st February 2008, 12:37
  5. Threaded server.
    By nithinin2001 in forum Qt Programming
    Replies: 2
    Last Post: 15th November 2007, 16:37

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.