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. QProcess waitForFinish exits immediately
QtWS25 Last Chance

QProcess waitForFinish exits immediately

Scheduled Pinned Locked Moved Solved General and Desktop
qprocesspythonscriptoutput
7 Posts 3 Posters 2.6k Views
  • 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.
  • K Offline
    K Offline
    Kyeiv
    wrote on 3 Jun 2020, 09:50 last edited by
    #1

    Hello!
    I have a script.py which i want to run with 2 arguments, this scripts after launching takes about 40 secs, then prints data tto standard output. The problem i am having is that waitForFinish doesn't wait till this script end but instead fires immediately and i cannot capture stdout. Is my code missing something?

    QSharedPointer<QProcess> process = new QProcess();
    QString execute_cmd = "script.py";
    
    process->start(execute_cmd, QStringList() << "-y" << "argg");
    process->waitForFinished(-1);
    
    if(process->exitStatus() == QProcess::ExitStatus::NormalExit)
    {
        QString result = process->readAllStandardOutput();
        // do sth
    }
    
    J 1 Reply Last reply 3 Jun 2020, 10:32
    0
    • K Kyeiv
      3 Jun 2020, 10:48

      sorry for not mentioning, i am using Ubuntu 18.04.
      how to correctly catch errors in this case?

      J Offline
      J Offline
      JonB
      wrote on 3 Jun 2020, 10:53 last edited by JonB 6 Mar 2020, 10:57
      #5

      @Kyeiv
      OK, then at least executing a .py should be possible, provided you have chmod +x-ed it. However, if you are default Ubuntu then . is not on your PATH, so I don't know where you think it's going to pick up the script.py from. Do you really execute that in a bash shell by just typing script.py, is it on your PATH?

      For errors, you have

      if(process->exitStatus() == QProcess::ExitStatus::NormalExit)
      

      why would you not look at the possible return results other than NormalExit and report them?

      Use process->readAllStandardError() at least, bash may be chatting to you there. There is also errorOccurred signal.

      K 1 Reply Last reply 3 Jun 2020, 10:58
      2
      • K Kyeiv
        3 Jun 2020, 09:50

        Hello!
        I have a script.py which i want to run with 2 arguments, this scripts after launching takes about 40 secs, then prints data tto standard output. The problem i am having is that waitForFinish doesn't wait till this script end but instead fires immediately and i cannot capture stdout. Is my code missing something?

        QSharedPointer<QProcess> process = new QProcess();
        QString execute_cmd = "script.py";
        
        process->start(execute_cmd, QStringList() << "-y" << "argg");
        process->waitForFinished(-1);
        
        if(process->exitStatus() == QProcess::ExitStatus::NormalExit)
        {
            QString result = process->readAllStandardOutput();
            // do sth
        }
        
        J Offline
        J Offline
        JonB
        wrote on 3 Jun 2020, 10:32 last edited by JonB 6 Mar 2020, 10:33
        #2

        @Kyeiv
        No idea, should work. Try running python/python3 as the command instead of relying on file association. Don't just check for "NormalExit", always check for errors, especially if you are having an issue. QProcess::waitForFinished() can be dodgy, especially if under Windows (and if you are under Windows how do you expect executing a .py file to work?), and for this question I'm surprised you don't think it's relevant to say what OS you are on. Consider whether QProcess::execute() might be an improvement on waitForFinished() anyway, or test it with proper signals & slots. Also test with a different Python script than whatever yours is. All of these are standard debugging techniques.

        1 Reply Last reply
        2
        • K Offline
          K Offline
          Kyeiv
          wrote on 3 Jun 2020, 10:48 last edited by
          #3

          sorry for not mentioning, i am using Ubuntu 18.04.
          how to correctly catch errors in this case?

          J J 2 Replies Last reply 3 Jun 2020, 10:52
          0
          • K Kyeiv
            3 Jun 2020, 10:48

            sorry for not mentioning, i am using Ubuntu 18.04.
            how to correctly catch errors in this case?

            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 3 Jun 2020, 10:52 last edited by
            #4

            @Kyeiv https://doc.qt.io/qt-5/qprocess.html#errorOccurred

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            2
            • K Kyeiv
              3 Jun 2020, 10:48

              sorry for not mentioning, i am using Ubuntu 18.04.
              how to correctly catch errors in this case?

              J Offline
              J Offline
              JonB
              wrote on 3 Jun 2020, 10:53 last edited by JonB 6 Mar 2020, 10:57
              #5

              @Kyeiv
              OK, then at least executing a .py should be possible, provided you have chmod +x-ed it. However, if you are default Ubuntu then . is not on your PATH, so I don't know where you think it's going to pick up the script.py from. Do you really execute that in a bash shell by just typing script.py, is it on your PATH?

              For errors, you have

              if(process->exitStatus() == QProcess::ExitStatus::NormalExit)
              

              why would you not look at the possible return results other than NormalExit and report them?

              Use process->readAllStandardError() at least, bash may be chatting to you there. There is also errorOccurred signal.

              K 1 Reply Last reply 3 Jun 2020, 10:58
              2
              • J JonB
                3 Jun 2020, 10:53

                @Kyeiv
                OK, then at least executing a .py should be possible, provided you have chmod +x-ed it. However, if you are default Ubuntu then . is not on your PATH, so I don't know where you think it's going to pick up the script.py from. Do you really execute that in a bash shell by just typing script.py, is it on your PATH?

                For errors, you have

                if(process->exitStatus() == QProcess::ExitStatus::NormalExit)
                

                why would you not look at the possible return results other than NormalExit and report them?

                Use process->readAllStandardError() at least, bash may be chatting to you there. There is also errorOccurred signal.

                K Offline
                K Offline
                Kyeiv
                wrote on 3 Jun 2020, 10:58 last edited by
                #6

                @JonB chmod +x solved issue, thank you for help! Closing.

                J 1 Reply Last reply 3 Jun 2020, 11:00
                0
                • K Kyeiv
                  3 Jun 2020, 10:58

                  @JonB chmod +x solved issue, thank you for help! Closing.

                  J Offline
                  J Offline
                  JonB
                  wrote on 3 Jun 2020, 11:00 last edited by JonB 6 Mar 2020, 11:00
                  #7

                  @Kyeiv
                  OK, but please put the error checking code in anyway. With OS commands you never know what might go wrong (just like you discovered), so it's important to check for & report all errors! And especially if you are going to be distributing this....

                  1 Reply Last reply
                  1

                  4/7

                  3 Jun 2020, 10:52

                  • Login

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