在 python 中的进程之间传递数据
pass data among processes in python
我正在尝试熟悉 subprocess.Popen 机制。
在下面的示例中,我尝试 运行 netstat 然后 运行 grep 输出。
netstat = subprocess.Popen("netstat -nptl".split(), stdout = subprocess.PIPE)
grep = subprocess.Popen("grep 192.168.46.134".split(), stdin = subprocess.PIPE)
然而,这不会产生所需的输出。
您需要将第一个进程的 stdout
引用为第二个进程的 stdin
:
import subprocess
netstat = subprocess.Popen("netstat -nptl".split(), stdout=subprocess.PIPE)
grep = subprocess.Popen("grep 192.168.46.134".split(), stdin=netstat.stdout, stdout=subprocess.PIPE)
stdout, stderr = grep.communicate()
print stdout # this is a string containing the output
我正在尝试熟悉 subprocess.Popen 机制。
在下面的示例中,我尝试 运行 netstat 然后 运行 grep 输出。
netstat = subprocess.Popen("netstat -nptl".split(), stdout = subprocess.PIPE)
grep = subprocess.Popen("grep 192.168.46.134".split(), stdin = subprocess.PIPE)
然而,这不会产生所需的输出。
您需要将第一个进程的 stdout
引用为第二个进程的 stdin
:
import subprocess
netstat = subprocess.Popen("netstat -nptl".split(), stdout=subprocess.PIPE)
grep = subprocess.Popen("grep 192.168.46.134".split(), stdin=netstat.stdout, stdout=subprocess.PIPE)
stdout, stderr = grep.communicate()
print stdout # this is a string containing the output