<div dir="ltr"><br><br>On Mon, Sep 8, 2008 at 4:43 PM, aha <<a href="mailto:aquil.abdullah@gmail.com">aquil.abdullah@gmail.com</a>> wrote:<br>> On Sep 8, 7:23 am, <a href="mailto:dudeja.ra...@gmail.com">dudeja.ra...@gmail.com</a> wrote:<br>
>> On Mon, Sep 8, 2008 at 11:50 AM,  <<a href="mailto:dudeja.ra...@gmail.com">dudeja.ra...@gmail.com</a>> wrote:<br>>> > Hi,<br>>><br>>> > I'm using the subprocess module's Popen() to start a batch file. This<br>
>> > batch file basically calls an exe which also gets started.<br>>> > Unfortunately, this does not produce any results. I looked into the<br>>> > Task bar that this exe has started but it does not consume and cpu so<br>
>> > I believet that this exe is not working.<br>>><br>>> > I used the following command to start the batch fiile:<br>>><br>>> > testing = subprocess.Popen([batchFilePath], \<br>>> >                                   shell = True, \<br>
>> >                                   stdout = subprocess.PIPE, \<br>>> >                                   stderr = subprocess.PIPE).communicate()[0]<br>>><br>>> > batchFilePath is the path of the batch file.<br>
>><br>>> > --<br>>> > Regrads,<br>>> > Rajat<br>>><br>>> Ok, I re-phrase my question:<br>>><br>>> there is a batch file that executes a exe file. The batch just works<br>
>> if run from command prompt and produces output to standard output and<br>>> the file.<br>>><br>>> Now, I try to call the same batch file from subprocess.Pope() call.<br>>> The batch file gets called and that internally also calls the exe<br>
>> file.<br>>><br>>> But, the exe just runs forever i.e. hangs and does not produces output<br>>> , atleast not to the file.<br>>><br>>> Please suggest is there is something wrong with the above code.<br>
>><br>>> Regards,<br>>> Rajat<br>><br>> Hello Rajat,<br>>  I would take a look at the thread below, it might help it might not:<br>><br>> <a href="http://groups.google.com/group/comp.lang.python/browse_thread/thread/4505613f014fdec7/3ee15c9c88a5efdc?hl=en#3ee15c9c88a5efdc">http://groups.google.com/group/comp.lang.python/browse_thread/thread/4505613f014fdec7/3ee15c9c88a5efdc?hl=en#3ee15c9c88a5efdc</a><br>
><br>> Also, if you post a larger section of the code I might be able to give<br>> you a hand. Once you've run the testing = subprocess.Popen()<br>><br>> make sure you use a testing.wait()<br>> --<br>
> <a href="http://mail.python.org/mailman/listinfo/python-list">http://mail.python.org/mailman/listinfo/python-list</a><br>><br>Hi Aaquil,<br><br>Thanks for helping me out with this piece of information.<div><br></div>
<div>My Original code is :</div><div><br></div><div><div><span class="Apple-style-span" style="font-family: 'courier new', monospace;">testing = subprocess.Popen([batchFilePath], \</span></div><div><span class="Apple-style-span" style="font-family: 'courier new', monospace;">                                   shell = True, \</span></div>
<div><span class="Apple-style-span" style="font-family: 'courier new', monospace;">                                   stdout = subprocess.PIPE, \</span></div><div><span class="Apple-style-span" style="font-family: 'courier new', monospace;">                                   stderr = subprocess.PIPE)</span></div>
<div><span class="Apple-style-span" style="font-family: 'courier new', monospace;">        </span></div><div><span class="Apple-style-span" style="font-family: 'courier new', monospace;">        result = testing.wait()</span></div>
<div><span class="Apple-style-span" style="font-family: 'courier new', monospace;">            </span></div><div><span class="Apple-style-span" style="font-family: 'courier new', monospace;">        if result < 0:</span></div>
<div><span class="Apple-style-span" style="font-family: 'courier new', monospace;">            childError = testing.stderr.read()</span></div><div><span class="Apple-style-span" style="font-family: 'courier new', monospace;">            tkMessageBox._show("Error", \</span></div>
<div><span class="Apple-style-span" style="font-family: 'courier new', monospace;">                                   icon='error', \</span></div><div><span class="Apple-style-span" style="font-family: 'courier new', monospace;">                                   message ="Error: %s" % childError)</span></div>
<div><span class="Apple-style-span" style="font-family: 'courier new', monospace;">            return None</span></div><div><span class="Apple-style-span" style="font-family: 'courier new', monospace;">        </span></div>
<div><span class="Apple-style-span" style="font-family: 'courier new', monospace;">        else:</span></div><div><span class="Apple-style-span" style="font-family: 'courier new', monospace;">            print result</span></div>
</div><div>My child process was unresponsive. Later I got to know with the below code that there were some errors coming from the child process which I was not able to detect with this code.</div><div><br></div><div>I googled for some solution and found the below code :-<br>
<br><span class="Apple-style-span" style="font-family: 'courier new', monospace;">print "ttt", batchFilePath<br>        #print os.listdir(batchFilePath)<br>        try:<br>            cmd = subprocess.Popen([batchFilePath], \<br>
                                   shell = True, \<br>                                   stdin = subprocess.PIPE,<br>                                   stdout = subprocess.PIPE, \<br>                                   stderr = subprocess.PIPE \<br>
                                   )<br>            cmd.stdin.close()<br>            outPipe, errPipe = PipeThread(cmd.stdout), PipeThread(cmd.stderr)<br>                            <br>            outPipe.run(), errPipe.run()<br>
            retcode = cmd.wait()<br>            out, err = outPipe.getOutput(), errPipe.getOutput()<br>            if retcode != 0 and err != '':<br>                raise ExecutionFailed, os.path.basename(batchFilePath) + " exited with error code " + str(retcode) + " and errors:\n" + err<br>
            elif retcode != 0:<br>                raise ExecutionFailed, os.path.basename(batchFilePath) + " exited with error code " + str(retcode) + " and output:\n" + out<br>            elif err != '':<br>
                return out + "\n" + os.path.basename(batchFilePath) + " gave warnings:\n" + err<br>            else:<br>                return out<br>        except Exception, e:<br>            if isinstance(e, ExecutionFailed):<br>
                raise<br>            else:<br>                raise ExecutionFailed, "Error while executing " +  ":\n" + str(e)</span><br>        <div>Here are the Exception  and the PipeThread Classes:</div>
<div><br></div><div><div><span class="Apple-style-span" style="font-family: 'courier new', monospace;">class PipeThread(threading.Thread):</span></div><div><span class="Apple-style-span" style="font-family: 'courier new', monospace;">    def __init__(self, fin):</span></div>
<div><span class="Apple-style-span" style="font-family: 'courier new', monospace;">        self.fin = fin</span></div><div><span class="Apple-style-span" style="font-family: 'courier new', monospace;">        self.sout = ""</span></div>
<div><span class="Apple-style-span" style="font-family: 'courier new', monospace;">        threading.Thread.__init__(self)</span></div><div><span class="Apple-style-span" style="font-family: 'courier new', monospace;">    def run(self):</span></div>
<div><span class="Apple-style-span" style="font-family: 'courier new', monospace;">        self.sout = self.fin.read()</span></div><div><span class="Apple-style-span" style="font-family: 'courier new', monospace;">    def getOutput(self):</span></div>
<div><span class="Apple-style-span" style="font-family: 'courier new', monospace;">        return self.sout</span></div><div><span class="Apple-style-span" style="font-family: 'courier new', monospace;">    </span></div>
<div><span class="Apple-style-span" style="font-family: 'courier new', monospace;">class ExecutionFailed(Exception):</span></div><div><span class="Apple-style-span" style="font-family: 'courier new', monospace;">    def __init__(self, value):</span></div>
<div><span class="Apple-style-span" style="font-family: 'courier new', monospace;">        self.parameter = value</span></div><div><span class="Apple-style-span" style="font-family: 'courier new', monospace;">    def __str__(self):</span></div>
<div><span class="Apple-style-span" style="font-family: 'courier new', monospace;">        return str(self.parameter)</span></div><div><span class="Apple-style-span" style="font-family: 'courier new'; font-size: 12px;"><br>
</span></div><div><span class="Apple-style-span" style="font-size: 12px;">Although, this solved my problem. But I know there is no need for using threading in this problem. This problem could've been solved just by the subprocess module.  </span></div>
<div><span class="Apple-style-span" style="font-size: 12px;"><br></span></div><div><span class="Apple-style-span" style="font-size: 12px;">I'm unnecessarily using the threading module.</span></div><div><span class="Apple-style-span" style="font-size: 12px;"><br>
</span></div><div><span class="Apple-style-span" style="font-size: 12px;">Regards,</span></div><div><span class="Apple-style-span" style="font-size: 12px;">Rajat</span></div><div><span class="Apple-style-span" style="font-size: 12px;"><br>
</span></div><div><span class="Apple-style-span" style="font-size: 12px;"><br></span></div><div><span class="Apple-style-span" style="font-family: 'courier new'; font-size: 12px;"><br></span></div></div><div><br><br>
-- <br>Regrads,<br>Rajat<br><br></div></div></div>