Thursday, March 1, 2012

Re: subprocess

On Thu, Mar 1, 2012 at 11:42 AM, Sébastien Billion
<sebastien.billion@gmail.com> wrote:
> Hi,
>
> You can try something like that:
>
> p = subprocess.Popen(args=[("%s %s") % (script_name,command_args1)],

Sorry to pick on something seemingly inconsequential, but this really
riles me. If you launch a subprocess using a string like "command arg1
arg2 arg3", then you force the OS to do additional work to spawn the
process via the shell. at which point the shell then has to re-parse
the arguments that you passed to it.

So, what's the big deal?

If command_args1 was the string 'hello world', then the suggested
approach would not work. The argument would be split into two separate
arguments by the shell. You already know which argument you want and
where you want it, so just pass them through appropriately with a
list.

The only 'benefit' going through a shell is that you can rely on the
shell searching $PATH to find the 'right' executable. As I've
intimated, this isn't much of a benefit.

OP:

Are you showing us precisely the code that runs? WFM…

>>> import sys
>>> import subprocess
>>> p=subprocess.Popen([sys.executable, 'hello.py', 'sdfg'], stdout=subprocess.PIPE)
>>> p.communicate()
('hello\n', None)


Cheers

Tom

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment