Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > get the en of a program running in background

Reply
Thread Tools

get the en of a program running in background

 
 
awalter1
Guest
Posts: n/a
 
      09-10-2006
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.

 
Reply With Quote
 
 
 
 
oripel
Guest
Posts: n/a
 
      09-10-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.


 
Reply With Quote
 
 
 
 
awalter1
Guest
Posts: n/a
 
      09-11-2006
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.
>
> 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.


 
Reply With Quote
 
awalter1
Guest
Posts: n/a
 
      09-11-2006
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.
>
> 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.


 
Reply With Quote
 
Damjan
Guest
Posts: n/a
 
      09-11-2006

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


--
damjan
 
Reply With Quote
 
awalter1
Guest
Posts: n/a
 
      09-11-2006
Hi,
I am using subprocess module, then I do not fork my program.
How use os.wait() in a non blocking mode ?
Thanks

Damjan wrote:
> > 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 .
>
>
> --
> damjan


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Background transparent when 'background' is used JWL HTML 4 09-26-2006 05:37 PM
Background Check - Background search - People search mason66 ASP .Net 0 07-27-2006 10:20 AM
Running a program in the background Muhammad Saqib Masood Python 0 04-01-2004 09:51 AM
running program as a background process ruthless@poczta.onet.pl C Programming 7 12-17-2003 11:26 PM
running program as a background process ruthless@poczta.onet.pl C++ 7 12-17-2003 11:26 PM



Advertisments