On Mon, Sep 8, 2008 at 4:43 PM, aha <[EMAIL PROTECTED]> wrote:
> On Sep 8, 7:23 am, [EMAIL PROTECTED] wrote:
>> On Mon, Sep 8, 2008 at 11:50 AM,  <[EMAIL PROTECTED]> wrote:
>> > Hi,
>>
>> > I'm using the subprocess module's Popen() to start a batch file. This
>> > batch file basically calls an exe which also gets started.
>> > Unfortunately, this does not produce any results. I looked into the
>> > Task bar that this exe has started but it does not consume and cpu so
>> > I believet that this exe is not working.
>>
>> > I used the following command to start the batch fiile:
>>
>> > testing = subprocess.Popen([batchFilePath], \
>> >                                   shell = True, \
>> >                                   stdout = subprocess.PIPE, \
>> >                                   stderr =
subprocess.PIPE).communicate()[0]
>>
>> > batchFilePath is the path of the batch file.
>>
>> > --
>> > Regrads,
>> > Rajat
>>
>> Ok, I re-phrase my question:
>>
>> there is a batch file that executes a exe file. The batch just works
>> if run from command prompt and produces output to standard output and
>> the file.
>>
>> Now, I try to call the same batch file from subprocess.Pope() call.
>> The batch file gets called and that internally also calls the exe
>> file.
>>
>> But, the exe just runs forever i.e. hangs and does not produces output
>> , atleast not to the file.
>>
>> Please suggest is there is something wrong with the above code.
>>
>> Regards,
>> Rajat
>
> Hello Rajat,
>  I would take a look at the thread below, it might help it might not:
>
>
http://groups.google.com/group/comp.lang.python/browse_thread/thread/4505613f014fdec7/3ee15c9c88a5efdc?hl=en#3ee15c9c88a5efdc
>
> Also, if you post a larger section of the code I might be able to give
> you a hand. Once you've run the testing = subprocess.Popen()
>
> make sure you use a testing.wait()
> --
> http://mail.python.org/mailman/listinfo/python-list
>
Hi Aaquil,

Thanks for helping me out with this piece of information.
My Original code is :

testing = subprocess.Popen([batchFilePath], \
                                   shell = True, \
                                   stdout = subprocess.PIPE, \
                                   stderr = subprocess.PIPE)

        result = testing.wait()

        if result < 0:
            childError = testing.stderr.read()
            tkMessageBox._show("Error", \
                                   icon='error', \
                                   message ="Error: %s" % childError)
            return None

        else:
            print result
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.

I googled for some solution and found the below code :-

print "ttt", batchFilePath
       #print os.listdir(batchFilePath)
       try:
           cmd = subprocess.Popen([batchFilePath], \
                                  shell = True, \
                                  stdin = subprocess.PIPE,
                                  stdout = subprocess.PIPE, \
                                  stderr = subprocess.PIPE \
                                  )
           cmd.stdin.close()
           outPipe, errPipe = PipeThread(cmd.stdout), PipeThread(cmd.stderr)

           outPipe.run(), errPipe.run()
           retcode = cmd.wait()
           out, err = outPipe.getOutput(), errPipe.getOutput()
           if retcode != 0 and err != '':
               raise ExecutionFailed, os.path.basename(batchFilePath) + "
exited with error code " + str(retcode) + " and errors:\n" + err
           elif retcode != 0:
               raise ExecutionFailed, os.path.basename(batchFilePath) + "
exited with error code " + str(retcode) + " and output:\n" + out
           elif err != '':
               return out + "\n" + os.path.basename(batchFilePath) + " gave
warnings:\n" + err
           else:
               return out
       except Exception, e:
           if isinstance(e, ExecutionFailed):
               raise
           else:
               raise ExecutionFailed, "Error while executing " +  ":\n" +
str(e)
       Here are the Exception  and the PipeThread Classes:

class PipeThread(threading.Thread):
    def __init__(self, fin):
        self.fin = fin
        self.sout = ""
        threading.Thread.__init__(self)
    def run(self):
        self.sout = self.fin.read()
    def getOutput(self):
        return self.sout

class ExecutionFailed(Exception):
    def __init__(self, value):
        self.parameter = value
    def __str__(self):
        return str(self.parameter)

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.

I'm unnecessarily using the threading module.

Regards,
Rajat





-- 
Regrads,
Rajat
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to