2

Using subprocess.Popen(), I'm launching a process that is supposed to take a long time. However, there is a chance that the process will fail shortly after it launches (producing a return code of 1). If that happens, I want to intercept the failure and present an explanatory message to the user. Is there a way to "listen" to the process and respond if it fails? I can't just use Popen.wait() because my python program has to keep running.

The hack I have in place right now is to time.sleep() my python program for .5 seconds (which should be enough time for the subprocess to to fail if it's going to do so). After the python program resumes, it polls the subprocess to determine if it has failed or not.

I imagine that a better solution might use threading and Popen.wait(), but I'm a relative beginner to python.

Edit: The subprocess is a Java daemon that I'm launching. If another instance of the daemon is already running on the system, the Java subprocess will exit with a return code of 1, and I want to intercept the messy Java exception stack trace and present an understandable error message to the user.

Jeremy
  • 225
  • 2
  • 11
  • 1
    So you essentially want `Popen.wait` with a timeout? – Christian Mann Aug 08 '12 at 19:57
  • Add some of your existing code – Qiau Aug 08 '12 at 19:57
  • Is it possible to handle the failure inside the subprocess? If not, can you give more details about what you're doing? – krethika Aug 08 '12 at 20:01
  • See [subprocess with timeout](http://stackoverflow.com/q/1191374/95735) – Piotr Dobrogost Aug 08 '12 at 21:24
  • A timeout would be helpful if the subprocess is supposed to return promptly but instead keeps running without returning anything. My situation is the other way around: the subprocess is supposed to keep running without returning anything, and I want to present an error message to the end user if it does return promptly (with a return code of 1). Is it possible to monitor stdout of the subprocess without stopping execution flow of my main python program? – Jeremy Aug 09 '12 at 22:44
  • By the way, the subprocess is a Java daemon that I'm launching. If another instance of the daemon is already running on the system, the Java subprocess will exit with a return code of 1, and I want to intercept the messy Java exception stack trace and present an understandable error message to the user. – Jeremy Aug 09 '12 at 22:47
  • What type of application are you running that a half-second wait feels like a full-blown stop?? – Wayne Werner Aug 10 '12 at 18:12
  • What else do you do in your code while the subprocess is running? – jdi Aug 10 '12 at 18:12
  • Wayne: A half-second wait is not the end of the world, but I was wondering if there is a more elegant solution. jdi: the Java subprocess is supposed to run for the rest of the python session. – Jeremy Aug 11 '12 at 19:10

1 Answers1

1

Two approaches:

  • Call Popen.wait() on a thread as you suggested yourself, then call an error handler function if the exit code is non-zero. Make sure that the error handler is thread safe, preferably by dispatching the error message to the main thread if your application has an event loop.
  • Rewrite your application to use an event loop that already supports monitoring child processes, such as pyev. If you just want to monitor one subprocess, this is probably overkill.
Soulman
  • 2,910
  • 24
  • 21