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

Kent Johnson kent37 at tds.net
Tue Sep 2 12:44:04 CEST 2008


2008/9/1 Colin. Wang <wkgogogo at gmail.com>:
> 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.

I think robocopy is blocked waiting to output; it can only output one
line every 5 seconds. Perhaps it times out. If you captured stderr you
might see error messages.

> 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)

You might be able to do this by doing a non-blocking read() of the
robocopy output but I don't know how to do that. Another option would
be to have a second timer thread that every five seconds looks to see
if the robocopy process is still running. The process that is reading
robocopy output could reset the timer when it gets some output. This
recipe is a starting point:
http://code.activestate.com/recipes/464959/

Kent


More information about the Tutor mailing list