我正在尝试使用subproccess和_thread模块运行命令.子进程有一个输出流.为了解决这个问题,我使用了两个线程,一个不断打印新线,另一个是检查输入.当我通过proc.stdin.write(‘Some string’)传递子进程输入时,它返回1然后我没有输出.沟通不能像我读过的大多数其他问题一样工作,因为它会阻止等待EOF,尽管它确实打印了将要返回的任何内容的第一行.我看到了一些使用’pty’的解决方案,但Windows不支持它.
@H_301_6@如果您想自己尝试,服务器文件夹中的文件只是一个Minecraft服务器.
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,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