正在编写一个IDE,使用subprocess.Popen时发现输出流会卡顿,请问为什么
def whileCheck():
while True:
if pypro.poll() != None:
#改变按钮和菜单状态
sb.config(state = tk.DISABLED)
rb.config(state = tk.NORMAL)
runfm.delete(0,1)
runfm.add_command(label = "运行",command = partial(runPythonFile,"run"))
runfm.add_command(label = "终止",command = partial(runPythonFile,"stop"),state = tk.DISABLED)
cst.config(state = tk.NORMAL)
cst.insert(tk.END,"\n\n--------------------Stop--------------------")
cst.config(state = tk.DISABLED)
cst.see(tk.END)
print("---子进程结束---")
return
def read_output(pipe,mod):
print(f"StartReadOutput:mod={mod}")
if mod == "err":
cst.tag_configure(mod, foreground="red")
else:
cst.tag_configure(mod, foreground="black")
while pypro.poll() is None:
line = pipe.readline().strip().decode("utf-8") + "\n"
if line:
print(f"std{mod} >> {line}",end = "")
#将输出写入到tk.text中
cst.config(state = tk.NORMAL)
cst.insert(tk.END,line,mod)
cst.config(state = tk.DISABLED)
cst.see(tk.END)
win.update()
pypro.stdout.close()
print(f"\n---{mod}输出流结束---\n")
pypro = subprocess.Popen([f"{pythonRunningPath}\\python37\\python.exe",openFileDir["path"]],
stdout = subprocess.PIPE,
stderr = subprocess.PIPE,
shell = False,
cwd = os.path.dirname(openFileDir["path"])
t1 = threading.Thread(target=read_output, args=(pypro.stdout,"out"))
t2 = threading.Thread(target=read_output, args=(pypro.stderr,"err"))
threading.Thread(target = whileCheck).start()
t1.start()
t2.start()