subprocess call is not waiting.

Hans Mulder hansmu at xs4all.nl
Wed Sep 19 09:23:49 EDT 2012


On 19/09/12 12:26:30, andrea crotti wrote:
> 2012/9/18 Dennis Lee Bieber <wlfraed at ix.netcom.com>:
>>
>>         Unless you have a really massive result set from that "ls", that
>> command probably ran so fast that it is blocked waiting for someone to
>> read the PIPE.
> 
> I tried also with "ls -lR /" and that definitively takes a while to run,
> when I do this:
> 
> proc = subprocess.Popen(['ls', '-lR', '/'], stdout=subprocess.PIPE,
> stderr=subprocess.PIPE)
> 
> nothing is running, only when I actually do
> proc.communicate()
> 
> I see the process running in top..
> Is it still an observation problem?

Yes: using "top" is an observation problem.

"Top", as the name suggests, shows only the most active processes.

It's quite possible that your 'ls' process is not active, because
it's waiting for your Python process to read some data from the pipe.

Try using "ps" instead.  Look in thte man page for the correct
options (they differ between platforms).  The default options do
not show all processes, so they may not show the process you're
looking for.

> Anyway I also need to know when the process is over while waiting, so
> probably a thread is the only way..

This sounds confused.

You don't need threads.  When 'ls' finishes, you'll read end-of-file
on the proc.stdout pipe.  You should then call proc.wait() to reap
its exit status (if you don't, you'll leave a zombie process).
Since the process has already finished, the proc.wait() call will
not actually do any waiting.


Hope this helps,

-- HansM





More information about the Python-list mailing list