1

I have a system call made from a Python script. I would like to have a timer as well as utilizing the output of the call. I was able to do one at a time: Implement a timer using subprocess.call() and retrieve the output using subprocess.Popen(). However, I need both timer and the output result.

Is there any way to achieve this?

Following code give me an Attribute error: 'int' object has no attribute 'stdout', because the subprocess.call output is not the Popen object I need to use.

... Open file here ...

try:
    result = subprocess.call(cmd, stdout=subprocess.PIPE, timeout=30)
    out = result.stdout.read()
    print (out)
except subprocess.TimeoutExpired as e:
    print ("Timed out!")

... Write to file here ...

Any help would be appreciated.

Yui Park
  • 289
  • 2
  • 4
  • 9

1 Answers1

1

In the documentation on subprocess.call() the one of the first things I noticed was:

Note:

Do not use stdout=PIPE or stderr=PIPE with this function. As the pipes are not being read in the current process, the child process may block if it generates enough output to a pipe to fill up the OS pipe buffer.

The next thing was the first line in the documentation

Run the command described by args. Wait for command to complete, then return the returncode attribute.

subprocess.call will return the "exit code", an int, generally 0 = success, 1 = something went wrong, etc.

For more infomation on exit codes...http://www.tldp.org/LDP/abs/html/exitcodes.html

Since you want the 'output'from your timer, you might want to revert to

timer_out = subprocess.Popen(command, shell=True, stdout=PIPE, stderr=PIPE, universal_newlines=True)
stout, sterror = timer_out.communicate()

...or something like it.

Nodak
  • 929
  • 1
  • 8
  • 14