subproccess 및 _thread 모듈을 사용하여 명령을 실행하려고합니다. 하위 프로세스에는 출력 스트림이 있습니다. 이 문제를 해결하기 위해 두 개의 스레드를 사용했습니다. 한 스레드는 끊임없이 새 행을 인쇄하고 다른 스레드는 입력을 검사합니다. proc.stdin.write('Some string')
을 통해 subprocess 입력을 전달하면 1을 반환하고 출력이 표시되지 않습니다. 통신문은 내가 읽은 대부분의 다른 질문에 따라 작동하지 않습니다. 왜냐하면 반환되는 내용의 첫 줄을 인쇄하지만 EOF를 기다리는 것을 차단하기 때문입니다. 'pty'를 사용하는 몇 가지 솔루션을 보았지만 Windows에서는 지원되지 않습니다.Windows에서 상수 출력을 사용하는 파이썬 블럭없는 서브 프로세스 입력
서버 폴더에있는 파일은 직접 시도하려는 경우에만 미니 크래프트 서버입니다. 한 번에 모든 입력을 전달하고 자식 프로세스가 종료 한 후 모든 출력을 얻을 수 있습니다 - 서브 프로세스
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
'_thread' 모듈을 사용하지 말고 대신'threading' 모듈을 사용하십시오. – jfs