如何向子进程发送新命令 [英] How to send a new command to a subprocess

查看:32
本文介绍了如何向子进程发送新命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前问过一个问题 关于如何设置 tkinter gui 以在不挂起整个程序的情况下从子进程接收行.现在可以正常使用了.

I previously asked a question about how to set up a tkinter gui to recieve lines from a subprocess without the entire program hanging. That is now functional.

现在,我不知道如何将新行发送到子进程.我曾尝试使用 process.communicate,但我可能一直在错误地使用它.我也试过 这个问题的解决方案,但 self.process.stdin.write('stop\n'.encode()) 似乎不起作用.如何向子 python 子进程发送新命令?

Now, I can't figure out how to send new lines to a subprocess. I've tried using process.communicate, but I might have been using it wrong. I also tried this question's solution, but self.process.stdin.write('stop\n'.encode()) doesn't seem to work. How do I send new commands to the child python subprocess?

相关代码:

self.process = subprocess.Popen([ "python", "-u", dir + "start.py" ], 
    stdout=subprocess.PIPE, 
    stdin=subprocess.PIPE, 
    stderr=subprocess.PIPE, 
    cwd=dir)

推荐答案

数据可能卡在管道中.写入后添加self.process.stdin.flush().

The data may be stuck in the pipe. Add self.process.stdin.flush() after the write.

这篇关于如何向子进程发送新命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆