Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #6076
| Date | 2011-05-23 16:13 +0100 |
|---|---|
| From | Tim Golden <mail@timgolden.me.uk> |
| Subject | Re: Problem in using subprocess module and communicate() |
| References | <BANLkTiktJHy2yrkdaGD3o-CCJ2i4CgtBsA@mail.gmail.com> <4DDA1CF5.60103@timgolden.me.uk> <BANLkTi=+OeZsezZvHSGd7J7kyWsGQ-Tc=w@mail.gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.1977.1306163625.9059.python-list@python.org> (permalink) |
[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:
<tjg.bat>
@echo Hello
</tjg.bat>
and here's a Python script which runs it:
<tjg.py>
import subprocess
subprocess.call ("tjg.bat", shell=True)
</tjg.py>
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:
<tjg.bat>
@echo off
echo Starting
python -c "import time; time.sleep (5)"
echo Finished
</tjg.bat>
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:
<tjg.py>
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"
</tjg.py>
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
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Problem in using subprocess module and communicate() Tim Golden <mail@timgolden.me.uk> - 2011-05-23 16:13 +0100
csiph-web