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

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 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
    }
    
    JonBJ 1 Reply Last reply
    0
    • K Kyeiv

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

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #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
      2
      • K Kyeiv

        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
        }
        
        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #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 last edited by
          #3

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

          jsulmJ JonBJ 2 Replies Last reply
          0
          • K Kyeiv

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

            jsulmJ Online
            jsulmJ Online
            jsulm
            Lifetime Qt Champion
            wrote on 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

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

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #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
              2
              • JonBJ JonB

                @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 last edited by
                #6

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

                JonBJ 1 Reply Last reply
                0
                • K Kyeiv

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

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #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

                  • Login

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