QProcess错误状态5是什么原因? [英] What is the reason for QProcess error status 5?

查看:731
本文介绍了QProcess错误状态5是什么原因?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个线程正在运行以下QProcess.他们随机地以错误状态5失败.Qt文档未提供更多详细信息.有谁知道该错误可能来自何处?非常感谢.

i have multiple threads running the following QProcess. Randomly they fail with error state 5. The Qt docs do not give any more details. Has anyone a clue what that error could come from? Thank you very much.

extCmd = new QProcess(this);

QString cmd = "/usr/bin/php";
QStringList argStr;
argStr << "/bin/sleep" << "10"; // changed to ever working command
extCmd->start(cmd, args);
bool suc = extCmd->waitForFinished(-1);
if (!suc) {
   qDebug() << "finishing failed error=" 
            << extCmd.error() 
            << extCmd.errorString();
}

给我输出:

finishing failed error= 5 "Unknown error"

推荐答案

与您的问题有关的是,您不应该在每个进程中都启动线程.完成后,QProcess发出finished(int code, QProcess::ExitStatus status)信号.成功启动和失败后,它还将分别发出started()error().将所有这三个信号连接到QObject的插槽中,然后开始该过程,并处理插槽中的结果.您不需要任何额外的线程.

Tangential to your problem is the fact that you should not be starting a thread per each process. A QProcess emits a finished(int code, QProcess::ExitStatus status) signal when it's done. It will also emit started() and error() upon successful and unsuccessful startup, respectively. Connect all those three signals to a slot in a QObject, then start the process, and deal with the results in the slots. You won't need any extra threads.

如果收到started()信号,则可以确保该进程的文件名正确,并且该进程已启动.无论您从finished(int)获取的退出代码是什么,它都表明该过程做了什么,也许是对您可能已传递给它的潜在无效参数的响应.如果收到error()信号,则说明由于您为QProcess::start()输入了错误的文件名或没有正确的权限,该过程无法启动.

If you get a started() signal, then you can be sure that the process's file name was correct, and the process was started. Whatever exit code you get from finished(int) is then indicative of what the process did, perhaps in response to potentially invalid arguments you might have passed to it. If you get a error() signal, the process has failed to start because you gave a wrong filename to QProcess::start(), or you don't have correct permissions.

您不应该在发生异步情况的地方编写同步代码.同步代码是阻止特定事件发生的代码,例如调用waitForCmdFinished.我希望有一个Qt配置标志可以禁用所有那些剩余的同步阻止API,就像有一个标志可以禁用/启用Qt 3支持API一样.这些阻塞API的仅可用性会导致像上面的代码这样的骇客骇客.默认情况下,应禁用这些API恕我直言.就像应该进行将QThread和派生类移动到另一个线程的测试一样.在我能找到的每个公开代码示例中,这也表明设计不良,而且我进行了彻底的搜索,以说服自己自己不疯狂或什么.

You should not be writing synchronous code where things happen asynchronously. Synchronous code is code that blocks for a particular thing to happen, like calling waitForCmdFinished. I wish that there was a Qt configuration flag that disables all those leftover synchronous blocking APIs, just like there's a flag to disable/enable Qt 3 support APIs. The mere availability of those blocking APIs promotes horrible hacks like the code above. Those APIs should be disabled by default IMHO. Just as there should be a test for moving QThread and derived classes to another thread. It's also a sign of bad design in every example of publicly available code I could find, and I did a rather thorough search to convince myself I wasn't crazy or something.

我回想起Qt中waitxxx方法的唯一合理用法是等待QThread完成.即使这样,也应该只在~QThread内部调用它,以防止在胎面仍在运行的情况下破坏QThread.

The only reasonable use I recall for a waitxxx method in Qt is the wait for a QThread to finish. Even then, this should be only called from within the ~QThread, so as to prevent the QThread from being destroyed with the tread still running.

这篇关于QProcess错误状态5是什么原因?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆