Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在Linux上使用pexpect从管道读取数据_Python_Python 3.x_Asynchronous_Python Asyncio_Pexpect - Fatal编程技术网

Python 在Linux上使用pexpect从管道读取数据

Python 在Linux上使用pexpect从管道读取数据,python,python-3.x,asynchronous,python-asyncio,pexpect,Python,Python 3.x,Asynchronous,Python Asyncio,Pexpect,我发布了一个与get from队列相关的新问题。这是代码(感谢Martijn Pieters) 要发送到管道,我使用以下代码: import pexpect test = r"/home/host/PycharmProjects/Tim/Tim.py" process = pexpect.spawn("python3 " + test) message = '{"header":{"protocolVersion":1,"messageID":2,"stationID":400},"cam"

我发布了一个与get from队列相关的新问题。这是代码(感谢Martijn Pieters)

要发送到管道,我使用以下代码:

import pexpect


test = r"/home/host/PycharmProjects/Tim/Tim.py"
process = pexpect.spawn("python3 " + test)
message = '{"header":{"protocolVersion":1,"messageID":2,"stationID":400},"cam":{"generationDeltaTime":1,"camParameters":{"basicContainer":{"stationType":5}}';
process.write(message + "\n")
process.wait()
但是我如何创建一个脚本来读而不是写呢? 我试过了

test = r"/home/host/PycharmProjects/Tim/Tim.py"
p = pexpect.spawn("python3 " + test, timeout=None)
while True:
    m = p.read()
    file = open(r"/home/host/Desktop/OpeListening.txt", "a")
    file.write(str(m))
    file.close()
p.wait()

但是读取会立即进入下一步,而不会显示任何消息。我的错误是什么

现在我用success Popen

process = subprocess.Popen(['python3', test], shell=False, stdout=subprocess.PIPE,
                           stdin=subprocess.PIPE, stderr=subprocess.STDOUT)
while True:
    result = process.stdout.readline()
    result = result.decode("utf-8")
    print(result)
proc.wait()
process = subprocess.Popen(['python3', test], shell=False, stdout=subprocess.PIPE,
                           stdin=subprocess.PIPE, stderr=subprocess.STDOUT)
while True:
    result = process.stdout.readline()
    result = result.decode("utf-8")
    print(result)
proc.wait()