Path: csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python': 0.09; 'exist.': 0.09; 'files"': 0.09; 'happens.': 0.09; 'stderr': 0.09; 'stdout': 0.09; 'subset': 0.09; 'wrong,': 0.09; 'cc:addr:python-list': 0.10; 'subject:not': 0.11; 'times,': 0.13; 'copies),': 0.16; 'datasets': 0.16; 'intended.': 0.16; 'loops': 0.16; 'proc': 0.16; 'wait.': 0.16; 'pfxlen:0': 0.17; 'successful,': 0.17; 'tries': 0.17; 'shell': 0.18; 'tests': 0.18; 'trying': 0.21; 'error.': 0.21; 'not,': 0.21; 'supposed': 0.21; 'tells': 0.22; 'runs': 0.22; 'cc:2**0': 0.23; "i've": 0.23; 'cc:no real name:2**0': 0.24; 'command': 0.24; 'script': 0.24; 'tried': 0.25; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; "doesn't": 0.28; 'run': 0.28; 'post': 0.28; 'fast.': 0.29; 'loop,': 0.29; 'url:mailman': 0.29; 'related': 0.30; 'connection': 0.30; 'returned': 0.30; 'code': 0.31; 'url:python': 0.32; 'file': 0.32; '-----': 0.32; 'etc.)': 0.32; 'quickly': 0.32; 'print': 0.32; 'url:listinfo': 0.32; 'skip:s 30': 0.33; 'like:': 0.33; 'turns': 0.33; 'times.': 0.33; 'server': 0.35; 'data,': 0.35; 'returning': 0.35; 'open': 0.35; 'remote': 0.35; 'something': 0.35; 'but': 0.36; 'url:org': 0.36; 'should': 0.36; 'possible': 0.37; 'why': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'nothing': 0.38; 'url:mail': 0.40; 'received:194': 0.61; 'back': 0.62; 'ever': 0.63; 'times': 0.63; 'useful.': 0.65; 'internet': 0.71; 'prompt': 0.78; '"too': 0.84; 'around,': 0.84; 'communicate,': 0.84 X-IronPort-AV: E=Sophos;i="4.80,417,1344204000"; d="scan'208";a="723215" X-Virus-Scanned: amavisd-new at zimbra.sequans.com Date: Thu, 13 Sep 2012 17:34:29 +0200 (CEST) From: Jean-Michel Pichavant To: paulstaten@gmail.com In-Reply-To: Subject: Re: subprocess call is not waiting. MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-Mailer: Zimbra 7.2.0_GA_2669 (ZimbraWebClient - GC7 (Linux)/7.2.0_GA_2669) Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 44 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1347550453 news.xs4all.nl 6843 [2001:888:2000:d::a6]:57897 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:29044 ----- Original Message ----- > I have a subprocess.call which tries to download a data from a remote > server using HTAR. I put the call in a while loop, which tests to > see if the download was successful, and if not, loops back around up > to five times, just in case my internet connection has a hiccup. > > Subprocess.call is supposed to wait. > > But it doesn't work as intended. The loop quickly runs 5 times, > starting a new htar command each time. After five times around, my > program tells me my download failed, because the target file doesn't > yet exist. But it turns out that the download is still > happening---five times. > > When I run htar from the shell, I don't get a shell prompt again > until after the download is complete. How come control is returned > to python before the htar command is through? > > I've tried using Popen with wait and/or communicate, but no waiting > ever happens. This is troublesome not only because I don't get to > post process my data, but because when I run this script for > multiple datasets (checking to see whether I have local copies), I > quickly get a "Too many open files" error. (I began working on that > by trying to use Popopen with fds_close, etc.) > > Should I just go back to os.system? > -- > http://mail.python.org/mailman/listinfo/python-list > A related subset of code would be useful. You can use subprocess.PIPE to redirect stdout & stderr et get them with communicate, something like: proc = subprocess.Popen(['whatever'], stdout=subprocess.PIPE, stdout=subprocess.PIPE) stdout, stderr = proc.communicate() print stdout print stderr Just by looking at stdout and stderr, you should be able to see why htar is returning so fast. JM PS : if you see nothing wrong, is it possible that htar runs asynchronously ?