473,395 Members | 1,790 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

get the en of a program running in background

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.

Sep 10 '06 #1
5 2549
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.
Sep 10 '06 #2
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.
Sep 11 '06 #3
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.
Sep 11 '06 #4
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
Sep 11 '06 #5
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
Sep 11 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

29
by: pb648174 | last post by:
I have a very long transaction that runs on the same database that other users need to use for existing data. I don't care if they see data from the transaction before it is done and am only using...
10
by: Nimit | last post by:
Hi, I wasn't sure which forum this post belongs to, so I've posted it to a couple forums that I thought may be appropriate. In giving me advice, please consider me a beginner. Below is a synopsis...
4
by: Xarky | last post by:
Hi, I have a program, that makes use of threads. Now when I quit the program from the upper right button of the window, the threads are not terminated and the program is still is running. How...
8
by: Sree | last post by:
hello friends, Can i make a C program run as as background service? for eg. like the windows services we have. which are not visible on the desktop but still running behind. If there is some...
2
by: Dan Tallent | last post by:
When a user launches the program, I want it to test if an update is available. You see many examples of this method to update. (Quickbooks, Norton AntiVirus, online games) I have a method...
4
by: Dylan Parry | last post by:
Hi folks, I'm writing a program that needs to execute an external program and wait for it to finish running before it can make use of the output from that program. Specifically, the external...
1
by: sewid | last post by:
Hi there! I've got a problem with no solution, I hope you might help me. I am writing a small tool with many buttons. Every button starts a thread and this thread starts something else, in the...
14
by: jmDesktop | last post by:
Hi, I'm trying to learn Python. I using Aquamac an emac implementation with mac os x. I have a program. If I go to the command prompt and type pythong myprog.py, it works. Can the program be...
1
by: jmDesktop | last post by:
I want to create my C# program to run in the background and detect when a program starts. In an interactive program, I know I can iterate through an array of Processes to find my process if I...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.