QProcess 执行 python3 脚本无法正常工作
QProcess executing python3 script not working properly
我在从 QProcess 执行 python3 脚本时遇到了这个问题。 python 脚本正在逐秒打印时间,它在命令行 下工作正常。在 Qt 中,信号 readyReadStandardOutput() 连接到一个插槽,在该插槽中调用 readAllStandardOutput() 以从脚本读取标准输出。 问题是插槽只被调用了一次!它打印了一次时间,然后就不再打印了。 QProcess 的 状态保持在 "running" 状态。 readyReadStandardError() 和 error(QProcess::ProcessError) 信号永远不会被调用。
为什么插槽只调用一次,而它必须每秒调用一次?谢谢
Python 脚本:
import time, threading
def foo():
print(time.ctime())
threading.Timer(1, foo).start()
foo()
Qt:
MClass::MClass(QObject* parent)
{
m_process = new QProcess(this);
connect(m_process, SIGNAL(readyReadStandardOutput()), this, SLOT(onTest()));
connect(m_process, SIGNAL(readyReadStandardError()), this, SLOT(onTestErr()));
connect(m_process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(onTestErr()));
connect(m_process, SIGNAL(stateChanged(QProcess::ProcessState)), this, SLOT(onTestState()));
startProcess();
}
void MClass::startProcess()
{
QString script= "../../python/test.py";
QString pythonCommand = "python3 " + script;
printf("PyCommand: %s\n", pythonCommand.toStdString().c_str());
m_process->start(pythonCommand);
// m_process->start("python3", QStringList()<<script);
}
}
void MClass::onTest()
{
qDebug()<<"------------------ON TEST";
while(m_process->canReadLine())
{
qDebug()<<m_process->readAllStandardOutput();
}
}
void MClass::onTestErr()
{
qDebug()<<"------------------ON ERR: " << m_process->errorString();
}
void MClass::onTestState()
{
qDebug()<<"------------------ STATE CHANGED: " << m_process->state();
}
我认为问题在于 python 标准输出的处理。我试过这个脚本:
def foo():
#print(time.ctime())
sys.stdout.write(time.ctime())
sys.stdout.write("\n")
sys.stdout.flush()
threading.Timer(1, foo).start()
我用 sys.stdout.write
替换了 print
并且它起作用了。我认为真正不同的是对 flush
的调用。
换句话说,你的脚本永远运行,所以标准输出永远不会被刷新。如果您尝试其他脚本,它会在五次迭代后结束:
def bar():
x = 0
while x < 5:
print(time.ctime())
time.sleep(1)
x = x + 1
在 Qt 应用程序中,您将在五秒后看到完整的输出。
我在从 QProcess 执行 python3 脚本时遇到了这个问题。 python 脚本正在逐秒打印时间,它在命令行 下工作正常。在 Qt 中,信号 readyReadStandardOutput() 连接到一个插槽,在该插槽中调用 readAllStandardOutput() 以从脚本读取标准输出。 问题是插槽只被调用了一次!它打印了一次时间,然后就不再打印了。 QProcess 的 状态保持在 "running" 状态。 readyReadStandardError() 和 error(QProcess::ProcessError) 信号永远不会被调用。
为什么插槽只调用一次,而它必须每秒调用一次?谢谢
Python 脚本:
import time, threading
def foo():
print(time.ctime())
threading.Timer(1, foo).start()
foo()
Qt:
MClass::MClass(QObject* parent)
{
m_process = new QProcess(this);
connect(m_process, SIGNAL(readyReadStandardOutput()), this, SLOT(onTest()));
connect(m_process, SIGNAL(readyReadStandardError()), this, SLOT(onTestErr()));
connect(m_process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(onTestErr()));
connect(m_process, SIGNAL(stateChanged(QProcess::ProcessState)), this, SLOT(onTestState()));
startProcess();
}
void MClass::startProcess()
{
QString script= "../../python/test.py";
QString pythonCommand = "python3 " + script;
printf("PyCommand: %s\n", pythonCommand.toStdString().c_str());
m_process->start(pythonCommand);
// m_process->start("python3", QStringList()<<script);
}
}
void MClass::onTest()
{
qDebug()<<"------------------ON TEST";
while(m_process->canReadLine())
{
qDebug()<<m_process->readAllStandardOutput();
}
}
void MClass::onTestErr()
{
qDebug()<<"------------------ON ERR: " << m_process->errorString();
}
void MClass::onTestState()
{
qDebug()<<"------------------ STATE CHANGED: " << m_process->state();
}
我认为问题在于 python 标准输出的处理。我试过这个脚本:
def foo():
#print(time.ctime())
sys.stdout.write(time.ctime())
sys.stdout.write("\n")
sys.stdout.flush()
threading.Timer(1, foo).start()
我用 sys.stdout.write
替换了 print
并且它起作用了。我认为真正不同的是对 flush
的调用。
换句话说,你的脚本永远运行,所以标准输出永远不会被刷新。如果您尝试其他脚本,它会在五次迭代后结束:
def bar():
x = 0
while x < 5:
print(time.ctime())
time.sleep(1)
x = x + 1
在 Qt 应用程序中,您将在五秒后看到完整的输出。