0

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.

  • 1
    `None` is a singleton; use `is None` instead of `== None`. – jfs Sep 18 '14 at 06:23
  • Thank you! Can you please explain the difference in short? – Yechiel Labunskiy Sep 18 '14 at 07:41
  • 1
    `is` compares objects identities. `==` is an equility. You could define your own custom class that overrides `__eq__` method so that its instances are equal (`==`) to `None` but only `None` is `None`. – jfs Sep 18 '14 at 07:45

1 Answers1

2

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.

Useless
  • 64,155
  • 6
  • 88
  • 132
  • That could be it, thanks! Will try at work tomorrow and let you know if that solves it. – Yechiel Labunskiy Sep 17 '14 at 20:46
  • 1
    @YechielLabunskiy: what made you use `PIPE` in the first place? You could [use `DEVNULL`, to discard output](http://stackoverflow.com/a/11270665/4279). – jfs Sep 18 '14 at 06:23