我正试图建立一个基于用户投入的终端站。 在产出产生后,我得以拿到产出,但读到正文。 在用户指挥系统执行后,是否可停读。 我增加了一个小的例子,以表明问题:
def communicate_with_subprocess(process, command):
try:
# Send the command to the subprocess
process.stdin.write(command +
)
process.stdin.flush()
lines = []
# Read the output from the subprocess
for _line in iter(process.stdout.readline, b ):
if _line == :
break
lines.append(_line)
return lines
except Exception as e:
print(f"Error executing command {command} : {e}")
return None
# Example usage:
subprocess_instance = subprocess.Popen(["/bin/bash"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
try:
while True:
user_input = input("Enter a command (type exit to quit): ")
if user_input.lower() == exit :
print("Exiting...")
break
output = communicate_with_subprocess(subprocess_instance, user_input)
print("Output:", output)
finally:
subprocess_instance.terminate()
subprocess_instance.wait()
目标是将其纳入Django网络应用,这样,如果存在任何涉及分处理的替代解决办法,这种解决办法也会奏效。