performance - Python subprocess module much slower than commands (deprecated) -


so wrote script accesses bunch of servers using nc on command line, , using python's commands module , calling commands.getoutput() , script ran in 45 seconds. since commands deprecated, want change on using subprocess module, script takes 2m45s run. have idea of why be?

what had before:

output = commands.getoutput("echo file.ext | nc -w 1 server.com port_num") 

now have

p = popen('echo file.ext | nc -w 1 server.com port_num', shell=true, stdout=pipe) output = p.communicate()[0] 

thanks in advance help!

i expect subprocess slower command. without meaning suggest only reason script running slowly, should take @ commands source code. there fewer 100 lines, , of work delegated functions os, many of taken straight c posix libraries (at least in posix systems). note commands unix-only, doesn't have work ensure cross-platform compatibility.

now take @ subprocess. there more 1500 lines, pure python, doing sorts of checks ensure consistent cross-platform behavior. based on this, expect subprocess run slower commands.

i timed 2 modules, , on quite basic, subprocess twice slow commands.

>>> %timeit commands.getoutput('echo "foo" | cat') 100 loops, best of 3: 3.02 ms per loop >>> %timeit subprocess.check_output('echo "foo" | cat', shell=true) 100 loops, best of 3: 5.76 ms per loop 

swiss suggests improvements script's performance. after applying them, note subprocess still slower.

>>> %timeit commands.getoutput('echo "foo" | cat') 100 loops, best of 3: 2.97 ms per loop >>> %timeit popen('cat', stdin=pipe, stdout=pipe).communicate('foo')[0] 100 loops, best of 3: 4.15 ms per loop 

assuming performing above command many times in row, add up, , account @ least of performance difference.

in case, interpreting question being relative performance of subprocess , command, rather being how speed script. latter question, swiss's answer better.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -