相关疑难解决方法(0)

Python子进程模块比命令慢得多(不建议使用)

所以我写了一个脚本,在命令行上使用nc访问一堆服务器,最初我使用Python的命令模块并调用commands.getoutput(),脚本在大约45秒内运行.由于命令已弃用,我想将所有内容更改为使用子进程模块,但现在脚本需要2m45s才能运行.任何人都知道为什么会这样?

我之前的所作所为:

output = commands.getoutput("echo get file.ext | nc -w 1 server.com port_num")
Run Code Online (Sandbox Code Playgroud)

我现在有

p = Popen('echo get file.ext | nc -w 1 server.com port_num', shell=True, stdout=PIPE)
output = p.communicate()[0]
Run Code Online (Sandbox Code Playgroud)

在此先感谢您的帮助!

python performance command subprocess

11
推荐指数
2
解决办法
2万
查看次数

标签 统计

command ×1

performance ×1

python ×1

subprocess ×1