Код: Выделить всё
import subprocess
import sys
import threading
def handle_output(shell):
while shell:
output = shell.stdout.readline(1)
print(output, end="")
sys.stdout.flush()
subprocess_shell = subprocess.Popen("cmd.exe", shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
threading.Thread(target=handle_output, args=(subprocess_shell,), daemon=True).start()
while True:
subprocess_shell.stdin.flush()
cmd = input()
subprocess_shell.stdin.write(cmd + "\n")
< /code>
Когда я выполняю такие команды, как Dir, Echo и т. Д. Это работает нормально, но как только я запускаю интерактивную программу, такую как Python < /code>, нет вывода. os.environ ['pythonunbuffered'] = '1'
Кажется, что это связано с несоблюренным outpupt , но настройка Os.environ ['pythonUnunBuffered , но настройка Os.environ [' pythonUnUnUNUNUNUNUNUNUNUNUNUNUN env = os.environ не подходит для всех ситуаций.
Код: Выделить всё
Python 3.10.6 (tags/v3.10.6:9c7b4bd, Aug 1 2022, 21:53:49) [MSC v.1932 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
Подробнее здесь: https://stackoverflow.com/questions/794 ... cess-popen