Я хочу использовать inkscape в режиме оболочки, потому что это намного быстрее. Я хочу запустить оболочку один раз, затем выполнить команды одну за другой, получить ответ от стандартного вывода и в конце закрыть интерактивную оболочку (с помощью выхода). Я нашел и адаптировал код, который предположительно делает это и предоставляет окно ввода для ввода команд. К сожалению, это вообще не работает и после ввода команды, которая не завершается, программа зависает на неопределенный срок. Как я могу получать отпечатки на консоль каждой команды? Я хочу, чтобы программа ждала, пока команда выдаст весь вывод, прежде чем прерывать цикл в send_command.
import subprocess
# Path to the Inkscape executable
INKSCAPE_EXE_PATH = r"C:\Program Files\Inkscape\bin\inkscape.exe"
# Start the Inkscape shell and redirect stdout and stderr to pipes
proc = subprocess.Popen(
[INKSCAPE_EXE_PATH, "--shell"],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
encoding='latin-1',
bufsize=1, # Line-buffered for reading output line by line
)
# Define a function to send a command to the subprocess and get its output
def send_command(command):
proc.stdin.write(command + '\n')
proc.stdin.flush()
output = ''
while True:
line = proc.stdout.readline()
if not line:
break
output += line
return output
# Interact with the subprocess
while True:
user_input = input("Enter a command (or 'exit' to quit): ")
if user_input == 'exit':
break
result = send_command(user_input)
print(result)
# Close the subprocess and wait for it to finish
proc.stdin.close()
proc.wait()
print("Subprocess exited with return code:", proc.returncode)
Подробнее здесь: https://stackoverflow.com/questions/790 ... tive-shell
Python 3.12 обрабатывает интерактивную оболочку ⇐ Python
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение