I wrote a script that accessed a bunch of servers using nc on the command line, and originally I was using Python’s commands module and calling commands.getoutput(). The script ran in about 45 seconds. Since commands is deprecated, I want to change everything over to using the subprocess module, but now the script takes 2 min 45 secs to run. Why would this be?
What I had before:
output = commands.getoutput("echo get file.ext | nc -w 1 server.com port_num")
Now I have
p = Popen('echo get file.ext | nc -w 1 server.com port_num', shell=True, stdout=PIPE)
output = p.communicate()[0]
I would expect
subprocessto be slower thancommand. Without meaning to suggest that this is the only reason your script is running slowly, you should take a look at thecommandssource code. There are fewer than 100 lines, and most of the work is delegated to functions fromos, many of which are taken straight from C POSIX libraries (at least in POSIX systems). Note thatcommandsis Unix-only, so it doesn’t have to do any extra work to ensure cross-platform compatibility.Now take a look at
subprocess. There are more than 1500 lines, all pure Python, doing all sorts of checks to ensure consistent cross-platform behavior. Based on this, I would expectsubprocessto run slower thancommands.I timed the two modules, and on something quite basic,
subprocesswas almost twice as slow ascommands.Swiss suggests some good improvements that will help your script’s performance. But even after applying them, note that
subprocessis still slower.Assuming you are performing the above command many times in a row, this will add up, and account for at least some of the performance difference.
In any case, I am interpreting your question as being about the relative performance of
subprocessandcommand, rather than being about how to speed up your script. For the latter question, Swiss’s answer is better.