Starting a third party application
johnm at cs.uit.no
johnm at cs.uit.no
Mon Feb 19 10:10:06 EST 2001
Arild Hansen <arildh at stud.cs.uit.no> writes:
> I'm trying to make a small module that executes a third party application
> (for example gcalc or whatever). I do this using
> os.system("applicationname &") and then the program enters a while loop
> waiting for some external signal. When this signal arrives, I want the
> program to make a sys.exit(1) and terminate itself AS WELL AS the third
> party application it started. The code looks something like:
>
> os.system("applicationame &")
> while continue:
> if signal:
> continue = FALSE
>
>
> My problem is that when the program gets the signal and terminates, the
> third party application is still running. How do I solve this problem?
Maybe you should take a look at fork, exec, kill and wait (or spawn if
you use one of the MS Windows variants). This example should do
something similar:
import os
import time
child = os.fork()
if child:
print "blabla"
time.sleep(2)
os.kill(child, 3) # or some other signal
os.wait(child)
else:
print "Child, starting /bin/sleep"
os.execv("/bin/sleep", ["/bin/sleep", "32"])
os.exit(-1) # Shouldn't happen, but in case exec fails
More information about the Python-list
mailing list