2017-08-27 37 views
2

我面臨的問題我到目前爲止面臨了幾次。 有問題的問題每次都得到解決,沒有我瞭解是什麼導致它 所以會發生什麼是我從我的c + +代碼啓動一個python虛擬環境。這有效,之後通過使用寫入功能,我可以在該環境中寫入東西。這到目前爲止還是非常好的。但是我無法將最後一條命令寫入流程。 我雖然關於也許有些緩衝區已滿,但我真的沒有發現有關在Qt的文檔Qt - 如何將更多的數據寫入QProcess?

這是相關的代碼的緩衝區什麼:

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); 

我把它包含一個日誌文件什麼被寫入到蟒蛇的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(); 
>>> 

它pront「OK寫」兩次,這在一個側面證明,我成功地寫我的命令的過程中,但我看不到任何東西。

正如你所看到的,測試句子「habelahabela」不會被寫入。 有沒有人知道我可能做錯了什麼? 我知道我正在編寫我的命令以便快速進入環境。因爲正如你所看到的,我首先編寫「import imp」,然後緩衝,稍後緩衝區被刷新,virtualenv執行命令(這就是爲什麼你會看到它兩次)。

有人明白我爲什麼看不到測試句,更重要的是我的實際命令"print myClassObj.addNewUser("+ name +");\n"被打印到虛擬環境中嗎?

感謝

+1

該緩衝區是[pipe(7)](http://man7.org/linux/man-pages/man7/pipe.7.html)。您可能需要處理[readyRead](http://doc.qt.io/qt-5/qiodevice.html#readyRead)&[readyReadStandardOutput](http://doc.qt.io/qt-5/qprocess) 。html#readyReadStandardOutput)Qt信號 –

+0

@BasileStarynkevitch確定thx澄清,你對我的問題有何建議? – LandonZeKepitelOfGreytBritn

+0

花幾個小時閱讀文檔。另請閱讀關於[操作系統]的教科書(http://pages.cs.wisc.edu/~remzi/OSTEP/) –

回答

2

首先,有書面while(!p->waitForBytesWritten());沒有意義的。 waitForBytesWritten已經在沒有while循環的情況下阻塞了您的線程,並且如名稱所示,等待直到寫入字節。僅當超時或錯誤時才返回false。在第一種情況下,你應該給它更多的時間來寫字節。在第二種情況下,您應該修復錯誤,然後再試一次。 這同樣適用於waitForStarted以及以「waitFor ...」開頭的所有其他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並多瞭解一點。