如何运行两个命令并查看输出。像这样的东西:
process.write('myVar = 2')
process.write('print myVar')
并查看每个命令的输出。如果我closeWriteChannel
查看输出,我就无法再与我的进程通信了。这是我的代码:
from PyQt4.QtCore import *
process = QProcess()
process.start('python -i')
process.waitForFinished(100)
process.readAllStandardOutput()
process.write('2+2')
process.closeWriteChannel()
process.waitForFinished(100)
print process.readAllStandardOutput()
答案 0 :(得分:1)
当有一些数据可供阅读时,您应该使用readyReadStandardOutput
的{{1}}和readyReadStandardError
信号获得通知。
如果你想发送两行,你只需要用换行符分隔它们。
示例:
QProcess
输出:
stderr: Python 2.7.4 (default, Apr 19 2013, 18:28:01) [GCC 4.7.3] on linux2 stderr: Type "help", "copyright", "credits" or "license" for more information. stderr: >>> stdout: 4 stderr: >>> stdout: 2 stderr: >>>
注意:不能保证from PyQt4.QtCore import *
app = QCoreApplication([])
process = QProcess()
def read_out():
print 'stdout:', str(process.readAllStandardOutput()).strip()
def read_err():
print 'stderr:', str(process.readAllStandardError()).strip()
process.readyReadStandardOutput.connect(read_out)
process.readyReadStandardError.connect(read_err)
process.start('python -i')
process.write('2+2\n')
process.write('1+1\n')
process.closeWriteChannel()
process.waitForFinished()
app.processEvents()
和4
的行是两个单独的读取。也可以
stderr: Python 2.7.4 (default, Apr 19 2013, 18:28:01) [GCC 4.7.3] on linux2 stderr: Type "help", "copyright", "credits" or "license" for more information. stdout: 4 2 stderr: >>> >>> >>>