subprocess returncode always None

Corey Wallis techxplorer at gmail.com
Sun Jul 9 04:22:55 EDT 2006


Dear All,

I'm currently working on a project that needs to collect the output of
the JHOVE application. More information about the application is
available at this website:

http://hul.harvard.edu/jhove/

The application is written in Java and is executed by a shell script.
There are occasions where this application may get stuck in an
infinite loop. For this reason I've been trying to implement a simple
class that I can use to execute the JHOVE application and if it
doesn't complete in the required period of time to raise an exception
and kill the process.

The class is as follows, apologies for the odd line wrapping.

<snip>
class niceSubprocess(object):
    """
    A class that implements a call to the subprocess method with a timeout
    - command, the command to execute
    - params, the parameters to pass to the application
    - timeout, the amount of time in seconds to wait
    """
    def executeCommand(self, command, params, timeout):
        try:
            timeElapsed = 0

            process = subprocess.Popen((command, params), stdin =
subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE,
close_fds = True)

            while process.poll() == None and timeElapsed < timeout:
                print process.poll()
                #print process.stdout.read()
                timeElapsed = timeElapsed + 1
                time.sleep(1)

            if process.poll() == None:
                # kill off the process, and don't be terribly nice about it
                os.kill(process.pid, signal.SIGKILL)
                raise timeoutError, timeout
            else:
                if process.stdout.read() == "":
                    raise executeError, process.stderr.read()
                else:
                    return process.stdout.read()

        except Exception, errorInfo:
            # pass the exception to the calling code
            raise errorInfo

</snip>

I'm running this on Linux and through the use of top can see the JAVA
process start and complete. The problem I have is that process.poll()
always returns None, even though the application has successfully ran
and returned output.

Can anyone shed some light on why the call to the shell script always
returns None as a return code?

With thanks.

-Corey

-- 
Corey Wallis
RUBRIC Technical Officer
University of Southern Queensland
http://www.rubric.edu.au
http://techxplorer.wordpress.com



More information about the Python-list mailing list