get the en of a program running in background

Discussion in 'Python' started by awalter1, Sep 10, 2006.

  1. awalter1

    awalter1 Guest

    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.
     
    awalter1, Sep 10, 2006
    #1
    1. Advertising

  2. awalter1

    oripel Guest

    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.
     
    oripel, Sep 10, 2006
    #2
    1. Advertising

  3. awalter1

    awalter1 Guest

    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.
     
    awalter1, Sep 11, 2006
    #3
  4. awalter1

    awalter1 Guest

    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.
     
    awalter1, Sep 11, 2006
    #4
  5. awalter1

    Damjan Guest


    > 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
     
    Damjan, Sep 11, 2006
    #5
  6. awalter1

    awalter1 Guest

    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
     
    awalter1, Sep 11, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    7
    Views:
    399
  2. Replies:
    7
    Views:
    515
  3. Muhammad Saqib Masood

    Running a program in the background

    Muhammad Saqib Masood, Apr 1, 2004, in forum: Python
    Replies:
    0
    Views:
    283
    Muhammad Saqib Masood
    Apr 1, 2004
  4. mason66
    Replies:
    0
    Views:
    430
    mason66
    Jul 27, 2006
  5. JWL
    Replies:
    4
    Views:
    596
    Michael Winter
    Sep 26, 2006
Loading...

Share This Page