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 this an efficient use of QProcess?

Is this an efficient use of QProcess?

Scheduled Pinned Locked Moved Solved General and Desktop
26 Posts 4 Posters 7.0k Views 1 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.
  • G Offline
    G Offline
    graniteDev
    wrote on last edited by
    #1

    What I have works, but I'm just curious if it's the best way to go about this. I'm re-using a process to execute various external .exe files and pass them arguments.

    process = new QProcess(this);
    process->execute(file, args);
    process->waitForFinish();
    process->close();
    
    process = new QProcess(this);
    process->execute(file2, args2);
    process->waitForFinish();
    process->close();
    
    etc.....
    

    Like I said this is working, but I'm just wondering if I'm being efficient or not. Maybe there is a better way to do this? I do need these processes to happen in sequence as well, so the forced wait and close and restart ensures the sequence is always correct.

    aha_1980A 1 Reply Last reply
    0
    • G graniteDev

      What I have works, but I'm just curious if it's the best way to go about this. I'm re-using a process to execute various external .exe files and pass them arguments.

      process = new QProcess(this);
      process->execute(file, args);
      process->waitForFinish();
      process->close();
      
      process = new QProcess(this);
      process->execute(file2, args2);
      process->waitForFinish();
      process->close();
      
      etc.....
      

      Like I said this is working, but I'm just wondering if I'm being efficient or not. Maybe there is a better way to do this? I do need these processes to happen in sequence as well, so the forced wait and close and restart ensures the sequence is always correct.

      aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @graniteDev

      if your code is in a thread, this is Ok. otherwise it would be better to connect the finished signal to a slot and start the next process from the to avoid blocking your event loop.

      Qt has to stay free or it will die.

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

        Hi,

        You are leaking Process objects. While you are giving them a parent, they won't be deleted until the parent object is so currently you might just be filling your memory.

        Why not just create one QProcess on the stack and then execute each command in sequence with that object ? You could even have a vector of QPair<QString, QStringList> that would be your commands with their respective parameters and use a for loop to execute them all.

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

        G 1 Reply Last reply
        4
        • aha_1980A aha_1980

          @graniteDev

          if your code is in a thread, this is Ok. otherwise it would be better to connect the finished signal to a slot and start the next process from the to avoid blocking your event loop.

          G Offline
          G Offline
          graniteDev
          wrote on last edited by
          #4

          @aha_1980

          No, it's not in a different thread. I've not worked with multi threading before so was unsure how to use it for this. It does lock up the system until it's done.

          1 Reply Last reply
          0
          • SGaistS SGaist

            Hi,

            You are leaking Process objects. While you are giving them a parent, they won't be deleted until the parent object is so currently you might just be filling your memory.

            Why not just create one QProcess on the stack and then execute each command in sequence with that object ? You could even have a vector of QPair<QString, QStringList> that would be your commands with their respective parameters and use a for loop to execute them all.

            G Offline
            G Offline
            graniteDev
            wrote on last edited by
            #5

            @SGaist can i execute different scripts and exe files off of one qprocess? I'm not sure how to do that...

            aha_1980A 1 Reply Last reply
            0
            • G graniteDev

              @SGaist can i execute different scripts and exe files off of one qprocess? I'm not sure how to do that...

              aha_1980A Offline
              aha_1980A Offline
              aha_1980
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @graniteDev yeah, should work.

              just leave out the subsequent new lines.

              @Lifetime-Qt-Champion well spotted , didn't see the leak...

              Qt has to stay free or it will die.

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

                I didn't realise... You are using execute which is a static method that runs the commands and waits for it to finish. Thus you are currently creating QProcess objects for nothing.

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

                G JonBJ 2 Replies Last reply
                4
                • SGaistS SGaist

                  I didn't realise... You are using execute which is a static method that runs the commands and waits for it to finish. Thus you are currently creating QProcess objects for nothing.

                  G Offline
                  G Offline
                  graniteDev
                  wrote on last edited by
                  #8

                  @SGaist please explain?

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

                    Static methods are not attached to an object, you should even have a compile warning telling you that you are calling a static method on an instance. See here for more details.

                    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
                    2
                    • SGaistS SGaist

                      I didn't realise... You are using execute which is a static method that runs the commands and waits for it to finish. Thus you are currently creating QProcess objects for nothing.

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by
                      #10

                      @SGaist said in Is this an efficient use of QProcess?:

                      I didn't realise... You are using execute which is a static method that runs the commands and waits for it to finish. Thus you are currently creating QProcess objects for nothing.

                      In which case, how come his process->waitForFinish(); even returns/doesn't error, because as you say the instance has never been start()ed?

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

                        waitForFinished has a timeout with a default value but in this case, since the process is not even running it will return early.

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

                        JonBJ 1 Reply Last reply
                        2
                        • G Offline
                          G Offline
                          graniteDev
                          wrote on last edited by
                          #12

                          I wonder if the confusion is that I left out the beginning code, the first process starts out:

                          QProcess *process = new QProcess(this);
                          process->execute(file1,args1);
                          process->waitforFinish();
                          process->close();
                          
                          process = new QProcess(this);
                          process->execute(file2,args2);
                          process->waitforFinish();
                          process->close();
                          
                          process = new QProcess(this);
                          process->execute(file3,args3);
                          process->waitforFinish();
                          process->close();
                          

                          I get no compile errors, and it's working. Per the Qt documentation close() deletes the object, so I don't believe I'm creating a memory leak with what I'm doing.

                          However, if I can eliminate creating a new object and then deleting it every time, I'm for improving the code.

                          1 Reply Last reply
                          0
                          • SGaistS SGaist

                            waitForFinished has a timeout with a default value but in this case, since the process is not even running it will return early.

                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on last edited by
                            #13

                            @SGaist
                            Ah, OK, I thought it might error "process not yet started".

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

                              @graniteDev Warning doesn't mean error unless you enabled "all warning be errors" for your compiler. As for close, it doesn't delete anything. It will kill the process that you started and close the communication channels. It's unrelated to memory management.

                              @JonB waitForFinished will return false because of that.

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

                              G 1 Reply Last reply
                              0
                              • SGaistS SGaist

                                @graniteDev Warning doesn't mean error unless you enabled "all warning be errors" for your compiler. As for close, it doesn't delete anything. It will kill the process that you started and close the communication channels. It's unrelated to memory management.

                                @JonB waitForFinished will return false because of that.

                                G Offline
                                G Offline
                                graniteDev
                                wrote on last edited by
                                #15

                                @SGaist Ok, how should I go about this properly then? Could you provide some example code?

                                1 Reply Last reply
                                0
                                • SGaistS Offline
                                  SGaistS Offline
                                  SGaist
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #16
                                  QProcess::execute(file1, args1);
                                  

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

                                  G 1 Reply Last reply
                                  2
                                  • SGaistS SGaist
                                    QProcess::execute(file1, args1);
                                    
                                    G Offline
                                    G Offline
                                    graniteDev
                                    wrote on last edited by
                                    #17

                                    @SGaist I could run these from a static function? How then do I know when they are finished if there is no object to emit the signal?

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

                                      Did you read the documentation of the function ? execute runs the command and waits for it to finish. The returned value indicates what happened.

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

                                      G 1 Reply Last reply
                                      3
                                      • SGaistS SGaist

                                        Did you read the documentation of the function ? execute runs the command and waits for it to finish. The returned value indicates what happened.

                                        G Offline
                                        G Offline
                                        graniteDev
                                        wrote on last edited by
                                        #19

                                        @SGaist yes but if i just run QProcess::execute(file1, args1); there is no object there for me to deal with.

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

                                          What do you want to deal with ? In the code you shown until now, what you are doing with your QProcess objects is already done "internally" by execute.

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

                                          G 1 Reply Last reply
                                          0

                                          • Login

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