get the en of a program running in background

oripel oripel at gmail.com
Sun Sep 10 23:42:48 CEST 2006


Module 'subprocess' may be a better fit for you than fork+exec.
Here's an example with a signal handler
(1) use subprocess, don't fork and exec
(2) maybe this will help:

---
import signal, subprocess

# define the signal handler
def logsignal(signum, frame):
   print "Caught signal"

# register the signal handler for SIGCHLD
signal.signal(signal.SIGCHLD, logsignal)

# run the subprocess in the background
subprocess.Popen(["sleep", "3"])

# Do more stuff
---

The signal handler will be called when the child process ends. Just
register your own handler.

You only need to register the handler once.
If you need this for a single run only, or need different behavior for
different subprocesses, have your signal handler re-register the old
handler (see the docs for module 'signal').

A note about the example: if you run it as is, the parent process will
end before the child process does. Add a call to 'os.wait()' to have it
wait for the child. In your GUI you probably won't want it.

Hope this helps.

awalter1 wrote:
> Hi,
> I develop a graphical user interface (with pyGTK) where a click on a
> button shall launch a program P in background. I want to get the end of
> this program P but I don't want that my HMI be freezed while P is
> running.
> I try to use fork examplesI found on the web, but it seems to not run
> as expected.
> I am not familiar with these techniques in unix as well as python.
> But I suppose that my needs are usual, despite that I don't find
> anything on the web ...
> Is someone can give me a standard way to call a background program,
> wait its end, with an IHM still active ?
> 
> Thank a lot for any idea.




More information about the Python-list mailing list