Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Is it safe to have QThreads launching QProcesses

Is it safe to have QThreads launching QProcesses

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 1.9k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • mbruelM Offline
    mbruelM Offline
    mbruel
    wrote on last edited by
    #1

    Hello,
    I'm going to develop an application that does several calculations (that can take few minutes or more). As it is possible to parallelize some of them I was thinking to have a Pool of working Threads (their number being dynamic) that would consume the CalculRequest from a shared queue.
    My problem is that some of the calculation will use an external program and thus would need to use a QProcess.
    I've been told that I'll have problems if I was mixing processes and threads and that I should have one dedicated process that would deal with all the subprocesses.
    Is it the case with QProcess or is the class handling totally the process and thus could be launched even from a thread?
    I know a QProcess wouldn't really need a thread but I then need to load the result of the process (from a file) in that thread. Does this make sense?

    1 Reply Last reply
    1
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      From an architectural point of view, does your process need to be started separately for each calculation or can you feed it with them ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • mbruelM Offline
        mbruelM Offline
        mbruel
        wrote on last edited by mbruel
        #3

        Yes it needs to be started each time. You call it with the path of a file where all its required parameters have been serialized in the format it understands and the name of the output file you'll then parse to get the result.

        People were telling me to not mix threads and processes that it would be a mess to maintain and could explode...
        Is it really the case when using QT wrappers or does it hide the complexity (checking process state, know when/how it is finished, prevent zombie process...)?
        That is why those persons where advising me to create one process that would manage all the calculation processes and keep the main process of the app for the HMI and other type of threads (Post treatments)
        What do you think about that? It kind of brings more complexity than my initial design...

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Prevent zombie process ? No, that's not Qt's role at all.

          QProcess does signal when the application started has excited.

          AFAIK, you can move QProcess objects to another thread.

          You might be interested by this KDAB article about the new QThread::create method in Qt 5.10.

          Depending on what you want to do with the output of the application you are calling through QProcess, you might want to consider a multi-stage pipeline:

          1. Creating the requests
          2. Handling these request through QProcess (no need of QThread)
          3. Handle the newly created results with for example QFileSystemWatcher.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • mbruelM Offline
            mbruelM Offline
            mbruel
            wrote on last edited by mbruel
            #5

            Thanks for your reply,
            As I said, sometimes I'll need to use an external program to do the calculation, sometimes not, the code will be embedded and thus run in a Thread.
            So I would like to make the nature of the task transparent to the user and even to the Simulation (object in my design) that launches the calculations (by filling a shared queue).
            I don't find much documentation on the web about the behaviour of a multi-Threads AND multi-Processes application.
            I've been told that it should be a NO go... (from C/ C++ Unix developers)
            I'd like to make sure that I could do such thing using QT: mixing QThreads and QProcesses in a same application or if there could be some risks.
            What do you think?
            Any links about mixing both?

            PS: @SGaist :

            • I'll have a look at this new way to create threads with QT 5.10, normally I should be able to use that version.
            • Idem for QFileSystemWatcher but I'm not sure to like the idea to dedicate a watcher when I can be signaled at the end of the process and thus know the exact moment I can do the reading. I could first run the QProcess then run a QThread to do read the result but I'd like to have the execution of both in one single task... Do you see my problem?
            S 1 Reply Last reply
            0
            • mbruelM mbruel

              Thanks for your reply,
              As I said, sometimes I'll need to use an external program to do the calculation, sometimes not, the code will be embedded and thus run in a Thread.
              So I would like to make the nature of the task transparent to the user and even to the Simulation (object in my design) that launches the calculations (by filling a shared queue).
              I don't find much documentation on the web about the behaviour of a multi-Threads AND multi-Processes application.
              I've been told that it should be a NO go... (from C/ C++ Unix developers)
              I'd like to make sure that I could do such thing using QT: mixing QThreads and QProcesses in a same application or if there could be some risks.
              What do you think?
              Any links about mixing both?

              PS: @SGaist :

              • I'll have a look at this new way to create threads with QT 5.10, normally I should be able to use that version.
              • Idem for QFileSystemWatcher but I'm not sure to like the idea to dedicate a watcher when I can be signaled at the end of the process and thus know the exact moment I can do the reading. I could first run the QProcess then run a QThread to do read the result but I'd like to have the execution of both in one single task... Do you see my problem?
              S Offline
              S Offline
              stevemcgf
              wrote on last edited by
              #6

              If you will wait on that thread until the process finish the operation I don't see any problem. Running external process multiple times very quickly is highly inefficient but you stated that each run will take at least one minute so the bottleneck wouldn't be on launching process.

              1 Reply Last reply
              1

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved