Friday, June 12, 2009

python subprocess module

Recent Python versions show DeprecatedWarning when using various popen functions and encourages to use subprocess module. And it's frustrating because I don't quite like the subprocess module. Let's take a simple example: executing one app and piping other app, like "dmesg|grep hda". They even have this example in the subprocess module documentation so I just took it. So, take a look:


output=`dmesg | grep hda`
==>
p1 = Popen(["dmesg"], stdout=PIPE)
p2 = Popen(["grep", "hda"], stdin=p1.stdout, stdout=PIPE)
output = p2.communicate()[0]


That looks lame to me. Look, original command has only 4 words used and is easy to understand. The second example has: 17 words, uses classes (who needs them here?) with some weird non intuitive constructor arguments and the final accord is "p2.communicate()[0]". What is "commmunicate()", can you guess what it does in details without looking at the docs? What does it return? What the hell is "[0]"? Seems really horrible to me.

Generally, it seems quite inconvenient to execute external applications using Python. Some people call Python 'a better shell', but it doesn't seem quite true if you need to execute external apps and in _most_ of shell scripts you really do need such things. Well, probably it's good at some sense that it forces you to split your Python logic and shell-scripting logic into python and shell scripts respectively and call shell scripts from your Python scripts and vice versa, but anyway, I don't like it a lot.

No comments:

Post a Comment