Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #6049

Re: Problem in using subprocess module and communicate()

Path csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <mail@timgolden.me.uk>
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; 'subject:module': 0.04; 'subject:using': 0.04; 'python': 0.08; '(possibly': 0.09; '(unless': 0.09; 'from:addr:timgolden.me.uk': 0.09; 'from:name:tim golden': 0.09; 'message-id:@timgolden.me.uk': 0.09; 'scripts,': 0.09; 'subject:()': 0.09; 'subject:Problem': 0.09; 'subprocess': 0.09; 'sure)': 0.09; 'variations': 0.09; 'scripts': 0.10; 'output': 0.11; 'def': 0.12; 'written': 0.14; 'wrote:': 0.14; 'guessing': 0.16; 'necessary;': 0.16; 'posts:': 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; 'runs,': 0.16; 'shell=true)': 0.16; 'stdin': 0.16; 'subject:subprocess': 0.16; 'threading': 0.16; 'input': 0.17; 'shell': 0.19; 'command': 0.19; 'header:In-Reply-To:1': 0.21; 'seems': 0.21; 'stuff': 0.22; 'trying': 0.23; 'short,': 0.23; 'code': 0.24; "doesn't": 0.25; 'pointed': 0.25; 'pass': 0.27; 'script': 0.27; "i'm": 0.27; '(the': 0.28; 'problem': 0.28; 'import': 0.29; 'matches': 0.29; 'code,': 0.29; 'skip:( 20': 0.30; "won't": 0.30; 'module': 0.30; 'harm': 0.30; 'invoke': 0.30; 'queue': 0.30; 'threads.': 0.30; 'tjg': 0.30; 'print': 0.31; 'seem': 0.32; 'clearly': 0.32; 'to:addr:python-list': 0.33; 'actually': 0.33; "isn't": 0.33; 'too': 0.33; "i've": 0.33; 'chris': 0.34; 'file': 0.34; 'describe': 0.34; 'there': 0.35; 'header:User-Agent:1': 0.35; 'closely': 0.35; 'here,': 0.35; 'using': 0.35; 'quite': 0.36; 'setting': 0.36; 'open': 0.36; 'probably': 0.36; 'requirements': 0.37; 'running': 0.37; 'assuming': 0.37; 'depend': 0.37; 'parallel': 0.37; 'response,': 0.37; 'could': 0.38; 'run': 0.38; 'but': 0.38; 'data': 0.38; 'docs': 0.38; 'ok,': 0.38; 'subject:: ': 0.38; 'some': 0.38; 'received:192': 0.38; 'easier': 0.39; 'correctly': 0.39; 'feed': 0.39; 'launch': 0.39; 'to:addr:python.org': 0.39; 'getting': 0.40; 'success': 0.60; 'kind': 0.60; 'your': 0.60; 'back': 0.63; 'from:addr:mail': 0.65; 'making': 0.67; 'feeding': 0.67; 'batch': 0.68; 'mountain': 0.68; 'afraid': 0.72; 'failure': 0.73; 'risk': 0.75; '"still': 0.84; 'touched': 0.91
Date Mon, 23 May 2011 09:38:13 +0100
From Tim Golden <mail@timgolden.me.uk>
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 <BANLkTiktJHy2yrkdaGD3o-CCJ2i4CgtBsA@mail.gmail.com>
In-Reply-To <BANLkTiktJHy2yrkdaGD3o-CCJ2i4CgtBsA@mail.gmail.com>
Content-Type text/plain; charset=windows-1252; 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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1960.1306139897.9059.python-list@python.org> (permalink)
Lines 74
NNTP-Posting-Host 82.94.164.166
X-Trace 1306139897 news.xs4all.nl 49047 [::ffff:82.94.164.166]:54869
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:6049

Show key headers only | View raw


On 21/05/2011 16:56, vijay swaminathan wrote:
> I'm having some problem in using the communicate() along with the
> subprocess.I would like to invoke a command prompt and pass  on a .bat
> file to execute. I went through the subprocess module and understood
> that using communicate, we can send the send data to stdin.

I'm afraid you're running the risk of giving us too little information
here, Vijay. You've pointed to the docs you've read and provided code,
which is A Good Thing. Unfortunately, the code you've provided is
clearly not what you'll ultimately be running (unless you're in the
game for a convoluted means of getting a directory listing) and, as
Chris touched on in his response, is probably not even what you're
running now, as you're passing the wrong kind of "/k" to cmd.exe.

OK, let's see if I've understood correctly from this and from your
previous posts:

* You have a set of batch scripts you want to launch from Python

* You want to launch them in parallel (possibly different scripts, not 
sure) so you're setting up threads.

You seem to be trying to meet these requirements by opening a
command shell with an arbitrary command, leaving it open and
feeding its stdin with the batch script name so that it runs,
and then polling it until it finishes.

In short, you're making a mountain out of a molehill. If you
need to run batch scripts from a command shell, then use
subprocess.call with the name of the batch script and shell=True
(the last isn't always necessary; it seems to depend on the
C runtime, but it won't harm to have it there):

<code>
import subprocess

result = subprocess.call ("name-of-script.bat", shell=True)

</code>

If you want to run that in a thread:

<code>
import subprocess
import threading

def run_script (path_to_script):
   subprocess.call (path_to_script, shell=True)

t = threading.Thread (target=run_script, args=("name-of-script.bat,"))
t.start ()

while True:
   # do stuff
   if t.isAlive ():
     print "still running"

</code>

You might, as you have above, want to launch a new console with each
subprocess or the output could get quite messy. There are lots of
variations on this theme: you could use a Queue to get the success or
failure back from the threads.

It's not clear whether you actually need to feed input into your
batch scripts, as the only reason you're using .communicate above
is to actually *run* the batch scripts.

I could go on guessing what your use-case is, assuming that it doesn't
involve a command shell with the output from a "dir" command, but it'll
be easier if you can describe how closely what I've written above
matches your need.

TJG

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Problem in using subprocess module and communicate() Tim Golden <mail@timgolden.me.uk> - 2011-05-23 09:38 +0100

csiph-web