473,716 Members | 2,475 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

subprocess leaves child living

Hi, When I do a small program like

from subprocess import Popen
popen = Popen(["ping", "google.com "])
from time import sleep
sleep(100)

start it and kill it, the ping process lives on.
Is there a way to ensure that the ping process is always killed when the
python process is?
I can't use atexit, as ping then isn't killed when python is killed "in
the hard way"
Jun 5 '07 #1
15 17560
Thomas Dybdahl Ahle schrieb:
Hi, When I do a small program like

from subprocess import Popen
popen = Popen(["ping", "google.com "])
from time import sleep
sleep(100)

start it and kill it, the ping process lives on.
Is there a way to ensure that the ping process is always killed when the
python process is?
I can't use atexit, as ping then isn't killed when python is killed "in
the hard way"
Calling popen.close() perhaps ?
You basically open a pipe, which spawns a shell and the command is then
started in there.
So, if your program quits, the spawned shell is still alive, only the
pipe is dead.
Jun 5 '07 #2
Den Tue, 05 Jun 2007 14:07:44 +0200 skrev Stefan Sonnenberg-Carstens:
Thomas Dybdahl Ahle schrieb:
>Hi, When I do a small program like

from subprocess import Popen
popen = Popen(["ping", "google.com "]) from time import sleep
sleep(100)

start it and kill it, the ping process lives on. Is there a way to
ensure that the ping process is always killed when the python process
is?
I can't use atexit, as ping then isn't killed when python is killed "in
the hard way"
Calling popen.close() perhaps ?
You basically open a pipe, which spawns a shell and the command is then
started in there.
So, if your program quits, the spawned shell is still alive, only the
pipe is dead.
Problem is - I can't do that when I get killed.
Isn't it possible to open processes in such a way like terminals? If I
kill the terminal, everything open in it will die too.
Jun 5 '07 #3

Thomas Dybdahl Ahle wrote:
Problem is - I can't do that when I get killed.
Isn't it possible to open processes in such a way like terminals? If I
kill the terminal, everything open in it will die too.
On POSIX platform you can use signals and ``os.kill`` function.
Fo example:

<code>
import os, signal
from subprocess import Popen
from time import sleep

def handler(signum, frame):
print 'Signal handler called'
raise KeyboardInterru pt

signal.signal(s ignal.SIGTERM, handler)

try:
popen = Popen(["ping", "google.com "])
try:
sleep(100)
except KeyboardInterru pt:
pass
finally:
if popen.poll() is None:
print "killing process: %d" % popen.pid
os.kill(popen.p id, signal.SIGTERM)
</code>

--
HTH,
Rob

Jun 5 '07 #4
Den Tue, 05 Jun 2007 07:06:15 -0700 skrev Rob Wolfe:
Thomas Dybdahl Ahle wrote:
>Problem is - I can't do that when I get killed. Isn't it possible to
open processes in such a way like terminals? If I kill the terminal,
everything open in it will die too.

On POSIX platform you can use signals and ``os.kill`` function. Fo
example:

<code>
import os, signal
from subprocess import Popen
from time import sleep

def handler(signum, frame):
print 'Signal handler called'
raise KeyboardInterru pt

signal.signal(s ignal.SIGTERM, handler)

try:
popen = Popen(["ping", "google.com "]) try:
sleep(100)
except KeyboardInterru pt:
pass
finally:
if popen.poll() is None:
print "killing process: %d" % popen.pid os.kill(popen.p id,
signal.SIGTERM)
</code>
But you can't ever catch sigkill.
Isn't there a way to make sure the os kills the childprocess when the
parrent dies?
Jun 5 '07 #5
Thomas Dybdahl Ahle <lo****@gmail.c omwrites:
But you can't ever catch sigkill.
There is no protection against sigkill.
Isn't there a way to make sure the os kills the childprocess when the
parrent dies?
If the parent dies suddenly without any notification childprocesses
become zombies and there is no way to avoid that.

--
HTH,
Rob
Jun 5 '07 #6

On Jun 5, 2007, at 3:01 PM, Rob Wolfe wrote:
Thomas Dybdahl Ahle <lo****@gmail.c omwrites:
>But you can't ever catch sigkill.

There is no protection against sigkill.
>Isn't there a way to make sure the os kills the childprocess when the
parrent dies?

If the parent dies suddenly without any notification childprocesses
become zombies and there is no way to avoid that.
Apologies for picking nits...

But actually *that* is an orphan process. When a parent process dies
and the child continues to run, the child becomes an orphan and is
adopted by init. Orphan processes can be cleaned up on most Unices
with 'init q' (or something very similar).

Zombies on the other hand, are those processes that have completed
execution but still have an entry in the process table. The cause of
zombies AFAIK, is a parent that has failed to call wait(2). To clean
up zombies, you can send a SIGCHLD signal to the parent process --
probably with 'kill -17' (but use 'kill -l' to find out what it is on
your system).

hth,
Michael
---
"I would rather use Java than Perl. And I'd rather be eaten by a
crocodile than use Java." — Trouser
Jun 5 '07 #7
Den Tue, 05 Jun 2007 22:01:44 +0200 skrev Rob Wolfe:
Thomas Dybdahl Ahle <lo****@gmail.c omwrites:
>But you can't ever catch sigkill.

There is no protection against sigkill.
>Isn't there a way to make sure the os kills the childprocess when the
parrent dies?

If the parent dies suddenly without any notification childprocesses
become zombies and there is no way to avoid that.
If I call "kill -9 pythonpid" on the python code you posted earlier, the
terminal running the script will continue pinging forever.
If it was a harder program like a chessengine or such, it would continue
consuming 100% cpu time.
Zombies would be fine to me.
Jun 5 '07 #8
Den Tue, 05 Jun 2007 15:46:39 -0500 skrev Michael Bentley:
But actually *that* is an orphan process. When a parent process dies
and the child continues to run, the child becomes an orphan and is
adopted by init. Orphan processes can be cleaned up on most Unices with
'init q' (or something very similar).
Is it not possible to tell python that this process should not be adopted
by init, but die with its parrent?
Just like terminals seem to do it..
Jun 5 '07 #9

On Jun 5, 2007, at 4:17 PM, Thomas Dybdahl Ahle wrote:
Den Tue, 05 Jun 2007 15:46:39 -0500 skrev Michael Bentley:
>But actually *that* is an orphan process. When a parent process dies
and the child continues to run, the child becomes an orphan and is
adopted by init. Orphan processes can be cleaned up on most
Unices with
'init q' (or something very similar).

Is it not possible to tell python that this process should not be
adopted
by init, but die with its parrent?
Just like terminals seem to do it..
Well, the way you posed the original question:
from subprocess import Popen
popen = Popen(["ping", "google.com "])
from time import sleep
sleep(100)
is really what adoption by init is designed to handle. Here you've
created a child but have not waited for its return value. Like a
good adoptive parent, init will wait(2) for the child.

I think if you really looked into it you'd find that the terminal had
called wait(2) before it was killed. Similarly, if you start a long-
running subprocess in python and wait for it to return -- killing the
parent will slaughter the child as well.

hth,
Michael
---
Let the wookie win.

Jun 5 '07 #10

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

Similar topics

0
2981
by: Robin Becker | last post by:
After struggling with os.spawnxxx to get a detached process I tried using Pyhton2.4's new subprocess module. I struggled with that as well even when trying to use the creation flags for DETACHED_PROCESS 0x8 and CREATE_NEW_PROCESS_GROUP = 0x200 I am using the following cgi script parent.cgi #!c:/python24/python.exe -u print 'content-type: text/plain'
5
2300
by: Robin Becker | last post by:
There seems to be a problem with calling subprocesses from a script run with pythonw rather than python. The error doesn't seem to be a function of using pythonw.exe rather than python.exe in the Popen call, but we seem to get an error when pythonw is used to execute the script proc0.py C:\Tmp>cat proc0.py #proc0.py import sys from subprocess import Popen, PIPE sys.stderr = sys.stdout = open('out.txt','w')
2
2343
by: MrEntropy | last post by:
Greetings, I'm having a little trouble getting an idea running. I am writing a C program which is really a frontend to a Python program. Now, my C program starts up, does some initialisation like initialisation of it's variables and Py_Initialize() and then it fork()s. After forking the child launches the python program with Py_Main() and with the parent I want to be able to read the variables of the Python program. I have tried many ways...
2
6275
by: Greg Ercolano | last post by:
When I use os.popen(cmd,'w'), I find that under windows, the stdout of the child process disappears, instead of appearing in the DOS window the script is invoked from. eg: C:\type foo.py import os import sys file = os.popen("nslookup", 'w') file.write("google.com\n") file.close()
9
6488
by: Phoe6 | last post by:
Hi all, Consider this scenario, where in I need to use subprocess to execute a command like 'ping 127.0.0.1' which will have a continuous non- terminating output in Linux. # code # This hangs at this point. How should I handle these kind of commands (ping 127.0.0.1) with
4
17523
by: 7stud | last post by:
Hi, What is the difference between: 1) getting the returncode directly from the subprocess object 2) calling poll() on the subprocess object? Here is an example:
12
4530
by: bhunter | last post by:
Hi, I've used subprocess with 2.4 several times to execute a process, wait for it to finish, and then look at its output. Now I want to spawn the process separately, later check to see if it's finished, and if it is look at its output. I may want to send a signal at some point to kill the process. This seems straightforward, but it doesn't seem to be working. Here's my test case:
7
6236
by: Samuel A. Falvo II | last post by:
I have a shell script script.sh that launches a Java process in the background using the &-operator, like so: #!/bin/bash java ... arguments here ... & In my Python code, I want to invoke this shell script using the Subprocess module. Here is my code: def resultFromRunning_(command):
2
3009
by: Chuckk Hubbard | last post by:
If I run 'python -i subprocessclient.py' I expect to see the nice level of it go up 2, and the nice level of the subprocess go up 1. But all I see is the nice level of the client change. What am I doing wrong? subprocessserver.py: ---------------------------- #!/usr/bin/python2.5 import os
0
8823
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9344
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9047
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7980
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6647
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4738
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3177
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2543
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2119
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.