Python 在Linux上使用pexpect从管道读取数据
我发布了一个与get from队列相关的新问题。这是代码(感谢Martijn Pieters) 要发送到管道,我使用以下代码: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"
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()