到目前为止,我正面临一个问题。 有问题的问题每次都会自行解决,而我却没有理解导致它的原因 那么我会从我的c ++代码开始一个python虚拟环境。这是有效的,之后通过使用写入功能,我能够在该环境中编写内容。到目前为止,这也完美无缺。但是我无法将最后一个命令写入该过程。 我虽然可能有一些缓冲区已满,但我在Qt docs中找不到任何关于缓冲区的内容
这是相关的代码:
static QStringList params;
QProcess *p = new QProcess();
params<<"-f"<<"-c"<<"python2"<< "/home/John/Desktop/python.log";
qDebug()<<"parameters: "<<params;
qDebug()<<"going to write";
p->start("script", params);
qDebug()<<"Turning on new user process...";
while(!p->waitForStarted())
{qDebug()<<"waiting for virtualenv to be ready";}
successFailWrite = p->write("import imp;\n");
while(!p->waitForBytesWritten());
successFailWrite = p->write("foo = imp.load_source('myTest', '/home/John/recognitionClass.py');\n");
while(!p->waitForBytesWritten());
successFailWrite = p->write("from myTest import recognitionClass;\n");
while(!p->waitForBytesWritten());
successFailWrite = p->write("myClassObj = recognitionClass();\n");
if(successFailWrite !=-1)
{qDebug()<<"OK written";}
while(!p->waitForBytesWritten());
successFailWrite = p->write("habelahabela\n");
if(successFailWrite !=-1)
{qDebug()<<"OK written";}
QString name = "John";
QString processNewUserParameter= "print myClassObj.addNewUser("+ name +");\n";
QByteArray processNewUserParameterByteArr= processNewUserParameter.toUtf8();
p->write(processNewUserParameterByteArr);
我保留一个日志文件,其中包含写入python virtualenv的内容以及正在打印的内容
Script started on Son 27 Aug 2017 20:09:52 CEST
import imp;
foo = imp.load_source('myTest', '/home/John/recognitionClass.py');
from myTest import recognitionClass;
myClassObj = recognitionClass();
Python 2.7.12 (default, Nov 19 2016, 06:48:10)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import imp;
>>> foo = imp.load_source('myTest', '/home/John/recognit
<myTest', '/home/John/recogniti onClass.py');
/usr/local/lib/python2.7/dist-packages/sklearn/cross_validation.py:44: DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. Also note that the interface of the new CV iterators are different from that of this module. This module will be removed in 0.20.
"This module will be removed in 0.20.", DeprecationWarning)
/usr/local/lib/python2.7/dist-packages/sklearn/grid_search.py:43: DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. This module will be removed in 0.20.
DeprecationWarning)
>>> from myTest import recognitionClass;
>>> myClassObj = recognitionClass();
>>>
它确实是“OK写”两次,这一方证明我成功地将命令写入了进程,但我看不到任何东西。
正如你所看到的那样,测试句“habelahabela”也没有写出来。 有没有人知道我可能做错了什么? 我知道我正在编写命令以快速进入环境。因为你可以看到我从编写“import imp”开始,然后它被缓冲,稍后缓冲区被刷新,virtualenv执行命令(这就是为什么你看到它两次)。
有人看到为什么我看不到测试句子 - 更重要的是 - 我的实际命令"print myClassObj.addNewUser("+ name +");\n"
被打印到虚拟环境中了吗?
由于
答案 0 :(得分:2)
首先,写作while(!p->waitForBytesWritten());
没有任何意义。 waitForBytesWritten
已经阻塞了没有while
循环的线程,并且如名称所示,等待直到写入字节。仅当存在超时或错误时,它才会返回false
。在第一种情况下,您应该给它更多时间来写入字节。在第二种情况下,您应该修复错误,然后再试一次。
同样适用于waitForStarted
以及以&#34; waitFor ...&#34;开头的所有其他Qt函数。
所以用法如下:
if(!p->waitForBytesWritten(-1)) // waits forever until bytes ARE written
{
qDebug() << "Error while writing bytes";
}
关于问题: 我认为问题(或至少是其中的一部分)是您将最后2条消息写入p
,但你既不会等待bytesWritten()
信号,也不会使用waitForBytesWritten()
阻止功能。虽然,可能没有发生错误(因为p->write(...)
此时不返回-1),但这并不意味着您的消息已经写入。简而言之,等待bytesWritten()
信号......
QProcess
继承自QIODevice
,因此我建议您查看its docs并了解更多内容。