[Tutor] monitor subprocess and get it's output during execution

Colin.Wang wkgogogo at gmail.com
Tue Sep 2 05:13:46 CEST 2008


2008/9/1 Kent Johnson <kent37 at tds.net>

> On Sun, Aug 31, 2008 at 11:39 PM, 王珂 <wkgogogo at gmail.com> wrote:
>
> > --Then I use:
> > print "Download begin ..."
> > p = subprocess.Popen(["robocopy.exe" +" " + "DownloadArgs"],Shell =
> > True,stdout = PIPE)
> > while not str(p.poll()).isdigit():
> >     print ">",
> >     time.sleep(10)
> >     #don't know how to grab output and display portion of it
> > print "Download complete with retval (%d) "% (p.poll())
> >
> > This will make robocopy never terminate, and display ">>>>>>" forever.
>
> I am not an expert in Popen, but I think in this example robocopy is
> blocked trying to output. Here is how to read the robocopy output:
>
> p = subprocess.Popen(["robocopy.exe" +" " + "DownloadArgs"],Shell =
> True,stdout = PIPE)
> while True:
>  line = p.stdout.readline()
>  if not line: break
>  print line
>
> Kent



Thanks Kent.
Your code works, and robocopy process exit when download complete.

When I try:
p = subprocess.Popen(["robocopy.exe" +" " + "DownloadArgs"],Shell =
True,stdout = PIPE)
while True:
 line = p.stdout.readline()
 if not line: break
 time.sleep(5) ### new added
 print ">",

then robocopy download will never complete.
It seems, when call sleep() here, the subprocess will sleep too.
Is that the cause? Thanks again.

When subprocess running, how can I print some character on console window on
a period of time? (Used to tell user the subprocess is still running)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20080902/8b7a7ca2/attachment.htm>


More information about the Tutor mailing list