get the en of a program running in background

A

awalter1

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.
 
O

oripel

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.
 
A

awalter1

Thanks a lot.
It does exactly what I expected and it's very simple

oripel a écrit :
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.
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.
 
A

awalter1

Hi,

It works when a click on a button launches a program P.
Now, I want that a click on another button launches another program P'

In this case there is only one signal for two events : the end of P and
the end of P'.
How can distinct the two cases.

In addition, what is the use of the frame parameter of the logsignal
procedure ?
It seems to be a little bit complicated to manipulate it.

Thank for your help


oripel a écrit :
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.
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.
 
D

Damjan

It works when a click on a button launches a program P.
Now, I want that a click on another button launches another program P'

In this case there is only one signal for two events : the end of P and
the end of P'.
How can distinct the two cases.

Remember the PIDs of the forked procesess and in your signal handler use
os.wait() to see which one has died.
BTW os.wait() can work in non-blocking mode .
 
A

awalter1

Hi,
I am using subprocess module, then I do not fork my program.
How use os.wait() in a non blocking mode ?
Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top