问题描述
我试图写有做了很多电话在一定的bash命令,解析和处理输出的最后给出一些输出的脚本。
我使用subprocess.Popen和subprocess.call
如果我理解正确的这些方法产生一个呸过程中,运行命令,获取输出,然后终止进程。
有没有办法让在后台不断地再蟒蛇通话可能只是直接进入该进程中运行的bash进程?这将是类似的bash作为服务器运行和Python会调用它。
我觉得这将优化调用了一下,因为没有bash进程安装和拆卸。还是会给出任何性能优势?
subprocess.Popen
is a bit more involved. It actually creates an I/O thread to avoid deadlocks. See https://www.python.org/dev/peps/pep-0324/:
Sure, you can still use subprocess.Popen
and send messages to you subprocess and receive messages back without terminating the subprocess. In the simplest case your messages can be lines.
This allows for request-response style protocols as well as publish-subscribe when the subprocess can keep sending you messages back when an event of interest happens.
这篇关于替代在Python子过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!