QProcess waitForFinish exits immediately
-
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 } -
sorry for not mentioning, i am using Ubuntu 18.04.
how to correctly catch errors in this case?@Kyeiv
OK, then at least executing a.pyshould be possible, provided you havechmod +x-ed it. However, if you are default Ubuntu then.is not on yourPATH, so I don't know where you think it's going to pick up thescript.pyfrom. Do you really execute that in abashshell by just typingscript.py, is it on yourPATH?For errors, you have
if(process->exitStatus() == QProcess::ExitStatus::NormalExit)why would you not look at the possible return results other than
NormalExitand report them?Use
process->readAllStandardError()at least,bashmay be chatting to you there. There is alsoerrorOccurredsignal. -
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 }@Kyeiv
No idea, should work. Try runningpython/python3as 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.pyfile to work?), and for this question I'm surprised you don't think it's relevant to say what OS you are on. Consider whetherQProcess::execute()might be an improvement onwaitForFinished()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. -
sorry for not mentioning, i am using Ubuntu 18.04.
how to correctly catch errors in this case? -
sorry for not mentioning, i am using Ubuntu 18.04.
how to correctly catch errors in this case?@Kyeiv
OK, then at least executing a.pyshould be possible, provided you havechmod +x-ed it. However, if you are default Ubuntu then.is not on yourPATH, so I don't know where you think it's going to pick up thescript.pyfrom. Do you really execute that in abashshell by just typingscript.py, is it on yourPATH?For errors, you have
if(process->exitStatus() == QProcess::ExitStatus::NormalExit)why would you not look at the possible return results other than
NormalExitand report them?Use
process->readAllStandardError()at least,bashmay be chatting to you there. There is alsoerrorOccurredsignal. -
@Kyeiv
OK, then at least executing a.pyshould be possible, provided you havechmod +x-ed it. However, if you are default Ubuntu then.is not on yourPATH, so I don't know where you think it's going to pick up thescript.pyfrom. Do you really execute that in abashshell by just typingscript.py, is it on yourPATH?For errors, you have
if(process->exitStatus() == QProcess::ExitStatus::NormalExit)why would you not look at the possible return results other than
NormalExitand report them?Use
process->readAllStandardError()at least,bashmay be chatting to you there. There is alsoerrorOccurredsignal. -