subprocess call is not waiting.

Chris Rebert clp2 at rebertia.com
Fri Sep 14 01:24:04 EDT 2012


On Thu, Sep 13, 2012 at 11:36 AM,  <paulstaten at gmail.com> wrote:
> Thanks, guys.
> MRAB-RedHat 6 64-bit, Python 2.6.5

In your Unix shell, what does the command:
    type htar
output?

> JM-Here's the relevant stuff from my last try.

If you could give a complete, self-contained example, it would assist
us in troubleshooting your problem.

> I've also tried with subprocess.call. Just now I tried shell=True, but it made no difference.

It's possible that htar uses some trickery to determine whether it's
being invoked from a terminal or by another program, and changes its
behavior accordingly, although I could not find any evidence of that
based on scanning its manpage.

> sticking a print(out) in there just prints a blank line in between each iteration. It's not until the 5 trials are finished that I am told: download failed, etc.
>
> from os.path import exists
> from subprocess import call
> from subprocess import Popen
> from shlex import split
> from time import sleep
>
> while (exists(file)==0) and (nTries < 5):

`file` is the name of a built-in type in Python; it should therefore
not be used as a variable name.
Also, one would normally write that as:
    while not exists(file) and nTries < 5:

>    a = Popen(split('htar -xvf ' + htarArgs), stdout=PIPE, stderr=PIPE)

What's the value of `htarArgs`? (with any sensitive parts anonymized)

Also, you really shouldn't use shlex.split() at run-time like that.
Unless `htarArgs` is already quoted/escaped, you'll get bad results
for many inputs. Use shlex.split() once at the interactive interpreter
to figure out the general form of the tokenization, then use the
static result in your program as a template.

>    (out,err) = a.communicate()
>    if exists(file)==0:
>       nTries += 1
>       sleep(0.5)
>
> if exists(file)==0: # now that the file should be moved
>    print('download failed: ' + file)
>    return 1
>
> I've also tried using shell=True with popopen.

I presume you meant Popen.

Cheers,
Chris



More information about the Python-list mailing list