Yechiel Labunskiy
Yechiel Labunskiy

Reputation: 427

Python - Executing code as long as a subprocess is running

I would like to run a section of code as long as a forked subprocess (rsync) is running. This is how I did it in my code:

rsync_proc = subprocess.Popen(proc_args, stdout=subprocess.PIPE)
while rsync_proc.poll() == None:
            sys.stdout.write('\r'+ 
                rsync_progress_report(source_size_kb, dest, start)),
            sys.stdout.flush()
            time.sleep(1)

For some reason, this causes the rsync subprocess to get stuck when it's almost finished. The while loop just continues looping with the rsync_proc.poll() returning None. When I do run this same rsync call without the while loop code, it finishes without a problem.

Thanks in advance.

Upvotes: 0

Views: 174

Answers (1)

Useless
Useless

Reputation: 67743

If you attach strace to your stuck rsync child process, you'll probably see it's blocked writing to stdout.

If it's blocked writing to stdout, it's probably because the pipe is full because you never read from it.

Try reading from the pipe and just discarding the output - or, if you really don't want the output, don't connect it to a pipe in the first place.

Upvotes: 2

Related Questions