Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'ok.': 0.04; 'subject:module': 0.04; 'subject:using': 0.04; 'python': 0.08; 'from:addr:timgolden.me.uk': 0.09; 'from:name:tim golden': 0.09; 'invokes': 0.09; 'message-id:@timgolden.me.uk': 0.09; 'subject:()': 0.09; 'subject:Problem': 0.09; 'subprocess': 0.09; 'scripts': 0.10; 'essentially': 0.11; 'output': 0.11; 'def': 0.12; 'wrote:': 0.14; '"import': 0.16; 'clear.': 0.16; 'covers.': 0.16; 'expected,': 0.16; 'flag,': 0.16; 'invoking': 0.16; 'list;': 0.16; 'mixture': 0.16; 'output?': 0.16; 'pause': 0.16; 'prompt?': 0.16; 'received:74.55.86': 0.16; 'received:74.55.86.74': 0.16; 'received:smtp.webfaction.com': 0.16; 'received:webfaction.com': 0.16; 'seconds,': 0.16; 'shell=true)': 0.16; 'sleeping': 0.16; 'subject:subprocess': 0.16; 'thread.': 0.16; 'threading': 0.16; 'worked.': 0.16; 'seconds': 0.16; "wouldn't": 0.17; 'command': 0.19; 'header:In-Reply-To:1': 0.21; 'appears': 0.21; '(usually': 0.23; 'runs': 0.23; 'code': 0.24; 'monitor': 0.26; 'script': 0.27; 'closing': 0.28; 'import': 0.29; "won't": 0.30; 'actions.': 0.30; 'invoke': 0.30; 'ran': 0.30; 'time;': 0.30; 'tjg': 0.30; 'specifically': 0.31; 'print': 0.31; 'does': 0.33; 'to:addr :python-list': 0.33; 'starting': 0.33; 'file': 0.34; 'one,': 0.34; 'that,': 0.34; 'all.': 0.35; 'header:User-Agent:1': 0.35; 'file:': 0.35; 'message.': 0.35; 'using': 0.35; 'session': 0.36; 'running': 0.37; 'something': 0.37; 'thread': 0.37; 'perform': 0.37; 'anything': 0.38; 'run': 0.38; 'but': 0.38; 'ok,': 0.38; 'subject:: ': 0.38; 'some': 0.38; 'received:192': 0.38; 'unless': 0.39; 'finished': 0.39; 'to:addr:python.org': 0.39; 'under': 0.40; 'generate': 0.60; 'more': 0.60; 'hope': 0.60; 'your': 0.60; 'below': 0.61; 'opened': 0.63; 'back': 0.63; 'from:addr:mail': 0.65; 'perfectly': 0.65; 'pop': 0.65; 'series': 0.66; 'batch': 0.68; 'care': 0.72; 'watching': 0.84; 'steps.': 0.93; 'baby': 0.95 Date: Mon, 23 May 2011 16:13:38 +0100 From: Tim Golden User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2.17) Gecko/20110414 Lightning/1.0b2 Thunderbird/3.1.10 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Problem in using subprocess module and communicate() References: <4DDA1CF5.60103@timgolden.me.uk> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 89 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1306163625 news.xs4all.nl 49181 [::ffff:82.94.164.166]:57152 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:6076 [cc-ing back to the list; please keep the conversation over there...] On 23/05/2011 13:11, vijay swaminathan wrote: > What I want to achieve is, I want to run a batch file on a command prompt. > > The reason for using thread is not for running multiple scripts > simultaneously. It is just to monitor my script running in command > prompt. only on closing the command prompt, I like to perform some more > actions. so I look to monitor my thread. Hope it is clear until now. Ok. That's perfectly clear. > > the below mentioned code does not invoke the command prompt at all. Does > the subprocess.call take care of invoking the command prompt? Adding shell=True invokes %COMSPEC% (usually cmd.exe) under the covers. How do you know it's not invoking the command prompt? Does your batch file generate output? And is that output generated? Without the CREATE_NEW_CONSOLE flag, you won't see an extra box pop up, but unless you specifically want one, then don't bother. OK, baby steps. Here's a batch file: @echo Hello and here's a Python script which runs it: import subprocess subprocess.call ("tjg.bat", shell=True) I opened a console (cmd.exe), ran tjg.py and, as expected, "Hello" appears in the same console. Note that, if I hadn't made the .bat file generate some output I wouldn't have seen anything but it would still have worked. I now make the .bat file do something more long-winded, such as fire up a Python session which waits for five seconds and then completes: @echo off echo Starting python -c "import time; time.sleep (5)" echo Finished When I run tjg.py again, I see "Starting" and then a pause of 5 seconds, and then "Finished". Now let's make the Python program monitor to see when that batch has finished by watching the isAlive status and then sleeping for a second: import subprocess import threading import time def run_tjg (): subprocess.call ("tjg.bat", shell=True) t = threading.Thread (target=run_tjg) t.start () while t.isAlive (): print "is alive" time.sleep (1) print "Thread is complete" When I run this, I get a mixture of output, depending on what gets to the console first, but essentially I see the batch file starting, I get a series of about 5 "is alive" messages, then the batch file "Finished" message and the Python "Thread is complete" message. Does that work for you? TJG