Python blocklose Subprozess-Eingabe mit konstanter Ausgabe unter Windows

Ich versuche, einen Befehl mit Unterprozess und den _thread-Modulen auszuführen. Der Unterprozess hat einen Ausgabestrom. Um dem entgegenzuwirken, habe ich zwei Threads verwendet, von denen einer ständig neue Zeilen druckt und der andere nach Eingaben sucht. Wenn ich die Subprozess-Eingabe durch @ leiproc.stdin.write('Some string') es gibt 1 zurück und dann bekomme ich keine Ausgabe. Die Kommunikation funktioniert nicht wie bei den meisten anderen Fragen, die ich gelesen habe, weil sie das Warten auf die EOF blockiert, obwohl die erste Zeile der zurückgegebenen Daten gedruckt wird. Ich habe einige Lösungen mit 'pty' gesehen, diese werden jedoch unter Windows nicht unterstützt.

Die Datei im Serverordner ist nur ein Minecraft-Server, wenn Sie es selbst ausprobieren möchten.

from subprocess import Popen,PIPE
import _thread
import sys
# asdf
proc = None
run = True
stdout = None
stdin = None


def getInput():
    global proc
    global run, stdin, stdout
    print("Proc inside the get input funct"+str(proc))
    inputs = input("Enter Something" + "\n")
    print("YOU ENTERED:", inputs)
    print("ATTEMPTING TO PIPE IT INTO THE CMD")
    run = True

    """----------------------------------------"""
    """        Works but blocks outputs        """
    """----------------------------------------"""
    # out,err=proc.communicate(bytes(inputs,'UTF-8'))
    # proc.stdin.flush()
    # print("Out is: "+out)




    """----------------------------------------"""
    """   Doesn't write but doesn't block      """
    """----------------------------------------"""
    # test = 0
    # test=proc.stdin.write(bytes(inputs,'UTF-8'))
    # print(test)
    # proc.stdin.flush()


def execute(command):
    global proc, stdin, stdout
    proc = Popen(command, cwd='C://Users//Derek//Desktop//server//',stdin=PIPE,stdout=PIPE,stderr=stdout, shell=True)
    lines_iterator = iter(proc.stdout.readline, "")
    print("Proc inside of the execute funct:"+str(proc))
    # print(lines_iterator)
    for line in lines_iterator:
        # print(str(line[2:-1]))
        # if line.decode('UTF-8') != '':
        print(line[:-2].decode('UTF-8')),  # yield line
        sys.stdout.flush()


threadTwo = _thread.start_new_thread(execute, (["java", "-jar", "minecraft_server.jar"], ))

while 1:
    if run and proc!=None:
        run = False
        threadOne = _thread.start_new_thread(getInput, ( ))

    pass

Antworten auf die Frage(2)

Ihre Antwort auf die Frage