473,326 Members | 2,133 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,326 software developers and data experts.

Killing children


I'm writing an application that has to spawn some processes and then
kill them later. It doesn't need to talk or listen to them while
they're running, but there are stop and start buttons and the stop
button should stop everything that gets started by the start button.

There are lots of ways to do this on Linux, but this project has to
run under cygwin. So far, the only thing I've gotten to work at all
under cygwin is doing an 'os.system' to start the process and then
parsing the "ps" output and using os.kill on the processes that ps
finds. This is unsatisfactory in a number of ways.

I have been unable to get os.spawnl to start the processes correctly.
I was using pexpect, which worked fine with test programs, but not
with the actual processes that need to be spawned. I did a little bit
of playing with the "process" module but didn't get it working right.

Does anyone have a better idea?
--
Laura (mailto:lc*****@laymusic.org , http://www.laymusic.org/ )
(617) 661-8097 fax: (501) 641-5011
233 Broadway, Cambridge, MA 02139
Jul 18 '05 #1
16 2959
Laura Conrad wrote:
I'm writing an application that has to spawn some processes and then
kill them later. It doesn't need to talk or listen to them while
they're running, but there are stop and start buttons and the stop
button should stop everything that gets started by the start button.

There are lots of ways to do this on Linux, but this project has to
run under cygwin. So far, the only thing I've gotten to work at all
under cygwin is doing an 'os.system' to start the process and then
parsing the "ps" output and using os.kill on the processes that ps
finds. This is unsatisfactory in a number of ways.

I have been unable to get os.spawnl to start the processes correctly.
I was using pexpect, which worked fine with test programs, but not
with the actual processes that need to be spawned. I did a little bit
of playing with the "process" module but didn't get it working right.

Does anyone have a better idea?


This has been successful in some cases:

if sys.platform == 'win32':
def _kill(pid, sig):
'''pid is actually handle, but that's what os.spawn* returns on
Win32'''
import win32api
win32api.TerminateProcess(pid, 0) # ignore sig, 0 is return
code for process
import os
os.kill = _kill
del _kill
It requires win32api, and I don't know how that all will work with
cygwin (which I don't use). Basically it lets you use os.kill()
in a platform-independent fashion (provided you care only about
Linux and Windows NT/XP/2K ;-).

-Peter
Jul 18 '05 #2
Hi Laura,

what a terrible subject line! ;-)

I sometimes use os.system with pskill from
http://www.sysinternals.com/ntw2k/freeware/pskill.shtml - this should
do what you want.

I do get my pid from some spawn-variant, IIRC. This should be more
reliable than killing by name or parsing ps output (since you might
kill the wrong process if there are multiple processes with the same
name).

What exactly doesn't work with your spawning? Could you post the spawn
call you're using?

Cheers,
Stefan

On 06.08.2004, at 19:36, Laura Conrad wrote:

I'm writing an application that has to spawn some processes and then
kill them later. It doesn't need to talk or listen to them while
they're running, but there are stop and start buttons and the stop
button should stop everything that gets started by the start button.

There are lots of ways to do this on Linux, but this project has to
run under cygwin. So far, the only thing I've gotten to work at all
under cygwin is doing an 'os.system' to start the process and then
parsing the "ps" output and using os.kill on the processes that ps
finds. This is unsatisfactory in a number of ways.

I have been unable to get os.spawnl to start the processes correctly.
I was using pexpect, which worked fine with test programs, but not
with the actual processes that need to be spawned. I did a little bit
of playing with the "process" module but didn't get it working right.

Does anyone have a better idea?
--
Laura (mailto:lc*****@laymusic.org , http://www.laymusic.org/ )
(617) 661-8097 fax: (501) 641-5011
233 Broadway, Cambridge, MA 02139
--
http://mail.python.org/mailman/listinfo/python-list

// st****@eischet.com //

Jul 18 '05 #3
>>>>> "Peter" == Peter Hansen <pe***@engcorp.com> writes:

Peter> on Win32'''
Peter> import win32api

Peter> It requires win32api, and I don't know how that all will work with
Peter> cygwin (which I don't use).

win32api doesn't seem to be there on cygwin.
--
Laura (mailto:lc*****@laymusic.org , http://www.laymusic.org/ )
(617) 661-8097 fax: (501) 641-5011
233 Broadway, Cambridge, MA 02139
Jul 18 '05 #4
Laura Conrad wrote:
>>"Peter" == Peter Hansen <pe***@engcorp.com> writes:

Peter> on Win32'''
Peter> import win32api

Peter> It requires win32api, and I don't know how that all will work with
Peter> cygwin (which I don't use).

win32api doesn't seem to be there on cygwin.


Nor on regular Python, until you download it. It's in a separate
package, formerly called win32all, now called pywin32...

-Peter
Jul 18 '05 #5
Jul 18 '05 #6
Laura Conrad wrote:
I used to think that when I first started using UNIX in the mid 80's.
But I am now a hardened child-killer.


I bet you even let them turn into zombies before you kill them! You
animal!

--
__ Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
/ \ San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
\__/ To be adult is to be alone.
-- Jean Rostand
Jul 18 '05 #7
Laura Conrad wrote:
p = os.spawnl(os.P_NOWAIT, "/usr/bin/python", "python", "hobo.py", '/dev/ttyS0', 5)
print p

os.system("ps")
time.sleep(60)
os.system("kill %d" % p)


This suggests that you believe the return value from os.spawnl() is
actually the PID. Unfortunately that doesn't appear to be the case
on Windows. See the note in the doc-string for my code sample from
the previous post (on Windows spawn* actually returns a "handle",
not the PID itself, and apparently turning the one into the other
is non-trivial).

-Peter
Jul 18 '05 #8
>>>>> "Peter" == Peter Hansen <pe***@engcorp.com> writes:

Peter> Laura Conrad wrote:
p = os.spawnl(os.P_NOWAIT, "/usr/bin/python", "python", "hobo.py", '/dev/ttyS0', 5)
print p
os.system("ps")
time.sleep(60)
os.system("kill %d" % p)


Peter> This suggests that you believe the return value from os.spawnl() is
Peter> actually the PID. Unfortunately that doesn't appear to be the case
Peter> on Windows. See the note in the doc-string for my code sample from
Peter> the previous post (on Windows spawn* actually returns a "handle",
Peter> not the PID itself, and apparently turning the one into the other
Peter> is non-trivial).

So you think os.kill(p) might be the way to kill it? But does anyone
know how to get it started right?
--
Laura (mailto:lc*****@laymusic.org , http://www.laymusic.org/ )
(617) 661-8097 fax: (501) 641-5011
233 Broadway, Cambridge, MA 02139
Jul 18 '05 #9
Laura Conrad wrote:
>>"Peter" == Peter Hansen <pe***@engcorp.com> writes:

Peter> Laura Conrad wrote: >> p = os.spawnl(os.P_NOWAIT, "/usr/bin/python", "python", "hobo.py", '/dev/ttyS0', 5)
>> print p
>> os.system("ps")
>> time.sleep(60)
>> os.system("kill %d" % p)


Peter> This suggests that you believe the return value from os.spawnl() is
Peter> actually the PID. Unfortunately that doesn't appear to be the case
Peter> on Windows. See the note in the doc-string for my code sample from
Peter> the previous post (on Windows spawn* actually returns a "handle",
Peter> not the PID itself, and apparently turning the one into the other
Peter> is non-trivial).

So you think os.kill(p) might be the way to kill it? But does anyone
know how to get it started right?


Uh... maybe? I think I just got out of my depth here... is Python
compiled specially for cygwin, or is it the standard Windows Python
that is involved?

If the latter, which I now doubt for some reason, then yes, os.kill()
can kill things spawn with spawnl/v/p/whatever provided that you use
the fake os.kill that I provided before, because os.kill doesn't
normally exist on Windows.

-Peter
Jul 18 '05 #10
On Fri, 06 Aug 2004 16:03:37 -0400, Laura Conrad <lc*****@laymusic.org>
declaimed the following in comp.lang.python:


The process it spawns doesn't appear on the ps, and no data gets put
in the database or written to stdout. When it tries to kill the
process it's spawned, I get an error message: "kill 2348: Operation not
permitted", which is different from the message I get if I just kill a
process that doesn't exist, so something is happening, but it isn't
what I want.
p = os.spawnl(os.P_NOWAIT, "/usr/bin/python", "python", "hobo.py",
'/dev/ttyS0', 5)

Pardon, I've not checked the details of os.spawnl() (or others)
but does it translate numeric arguments (the 5) to a format usable for
argv (if C) command line?

Maybe the spawned process is dying on its own before you can
kill it because it wants a quoted '5'?

-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Jul 18 '05 #11
>>>>> "Dennis" == Dennis Lee Bieber <wl*****@ix.netcom.com> writes:

Dennis> p = os.spawnl(os.P_NOWAIT, "/usr/bin/python", "python", "hobo.py",
Dennis> '/dev/ttyS0', 5)

Dennis> Pardon, I've not checked the details of os.spawnl()
Dennis> (or others) but does it translate numeric arguments (the
Dennis> 5) to a format usable for argv (if C) command line?

Dennis> Maybe the spawned process is dying on its own before you can
Dennis> kill it because it wants a quoted '5'?

That was it; thanks.

--
Laura (mailto:lc*****@laymusic.org , http://www.laymusic.org/ )
(617) 661-8097 fax: (501) 641-5011
233 Broadway, Cambridge, MA 02139
Jul 18 '05 #12

On 06.08.2004, at 22:03, Laura Conrad wrote:
>> "Stefan" == Stefan Eischet <st****@eischet.com> writes:


Stefan> I sometimes use os.system with pskill from
Stefan> http://www.sysinternals.com/ntw2k/freeware/pskill.shtml -
Stefan> this should do what you want.

I don't see that it really saves me much over using the cygwin kill
command.


Right. The program I use this in doesn't run on cygwin but on "normal"
win32, so I have no kill command there.

Another poster mentioned the possible difference between handles and
pids, which I didn't notice in my case. I only feed the number returned
from the spawn call into pskill, which just does the right thing.

Stefan

// st****@eischet.com //

Jul 18 '05 #13

Further adventures in infanticide. I've been spawning and killing
programs all day, in the course of testing and fixing other aspects of
the program.

All of a sudden, although I haven't changed the spawn line at all,
only things farther along in the program, I get:

C:\cygwin\bin\python2.3.exe (5684): *** unable to remap C:\cygwin\bin\cygcrypto-0.9.7.dll to same address as parent(0x6B0000) != 0x6C0000
4 [main] python 5468 sync_with_child: child 5684(0x710) died before initialization with status code 0x1
681 [main] python 5468 sync_with_child: *** child state child loading dlls
Traceback (most recent call last):
File "testspawn.py", line 8, in ?
p = os.spawnl(os.P_NOWAIT, "/usr/bin/python", "python", 'run_rad7.py', "/dev/ttyS8", "120", "0", "00560")
File "/usr/lib/python2.3/os.py", line 566, in spawnl
return spawnv(mode, file, args)
File "/usr/lib/python2.3/os.py", line 521, in spawnv
return _spawnvef(mode, file, args, None, execv)
File "/usr/lib/python2.3/os.py", line 489, in _spawnvef
pid = fork()
OSError: [Errno 11] Resource temporarily unavailable

This is the same kind of error that caused me to stop using
pexpect.spawn. Does anyone know what's happening? rebooting does not
cause it to go away. The program I attempting to spawn from my python
program runs fine from the command line.
--
Laura (mailto:lc*****@laymusic.org , http://www.laymusic.org/ )
(617) 661-8097 fax: (501) 641-5011
233 Broadway, Cambridge, MA 02139
Jul 18 '05 #14
On Fri, 06 Aug 2004 16:03:37 -0400, Laura Conrad
<lc*****@laymusic.org> wrote:
Stefan> Hi Laura,
Stefan> what a terrible subject line! ;-)

I used to think that when I first started using UNIX in the mid 80's.
But I am now a hardened child-killer.


As for the subject line; I thought at first glance, it was spam.

strange kind of humor.

SCNR
--
Franz Steinhaeusler
Jul 18 '05 #15

It looks like there is no space left to fork the subprocess and
load/map the libraries the latter needs.

Maybe there too many processes running already, simultaneously?

/Jean Brouwers
In article <87************@debian.laymusic.org>, Laura Conrad
<lc*****@laymusic.org> wrote:
Further adventures in infanticide. I've been spawning and killing
programs all day, in the course of testing and fixing other aspects of
the program.

All of a sudden, although I haven't changed the spawn line at all,
only things farther along in the program, I get:

C:\cygwin\bin\python2.3.exe (5684): *** unable to remap
C:\cygwin\bin\cygcrypto-0.9.7.dll to same address as parent(0x6B0000) !=
0x6C0000
4 [main] python 5468 sync_with_child: child 5684(0x710) died before
initialization with status code 0x1
681 [main] python 5468 sync_with_child: *** child state child loading dlls
Traceback (most recent call last):
File "testspawn.py", line 8, in ?
p = os.spawnl(os.P_NOWAIT, "/usr/bin/python", "python", 'run_rad7.py',
"/dev/ttyS8", "120", "0", "00560")
File "/usr/lib/python2.3/os.py", line 566, in spawnl
return spawnv(mode, file, args)
File "/usr/lib/python2.3/os.py", line 521, in spawnv
return _spawnvef(mode, file, args, None, execv)
File "/usr/lib/python2.3/os.py", line 489, in _spawnvef
pid = fork()
OSError: [Errno 11] Resource temporarily unavailable

This is the same kind of error that caused me to stop using
pexpect.spawn. Does anyone know what's happening? rebooting does not
cause it to go away. The program I attempting to spawn from my python
program runs fine from the command line.

Jul 18 '05 #16
>>>>> "Jean" == Jean Brouwers <JB***********************@no.spam.net> writes:

Jean> It looks like there is no space left to fork the subprocess and
Jean> load/map the libraries the latter needs.

Jean> Maybe there too many processes running already, simultaneously?

But if that was it, rebooting would have fixed it, wouldn't it?

--
Laura (mailto:lc*****@laymusic.org , http://www.laymusic.org/ )
(617) 661-8097 fax: (501) 641-5011
233 Broadway, Cambridge, MA 02139
Jul 18 '05 #17

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

Similar topics

6
by: Colin Steadman | last post by:
I have created a function to kill all session variables that aren't in a safe list. This is the function - Sub PurgeSessionVariables For Each Item In Session.Contents Select Case Trim(Item)...
1
by: Todd Cary | last post by:
In my previous post, I stated that some JavaScript I inherited does not work in Firefox but does work in IE 6 . I have now isolated a line where it fails in Firefox; the line with the property of...
6
by: Luke Dalessandro | last post by:
I'm not sure if this is the correct forum for platform specific (Mozilla/Firefox) javascript problems, so just shout and point me to the correct newsgroup if I'm being bad. Here's the deal... ...
1
by: krose | last post by:
Hi Using a couple of examples on CodeProject.com I've create a nice little shell extension (adds some custom menus for proprietary file types). Everything is working fine, except I can't get it...
0
by: Walt Borders | last post by:
Hi, My problem: Merging two datasets deletes parent elements, preserves all children. I've created two dataSets. Each use the same schema, parent-child nested tables. The first dataSet is...
39
by: clintonG | last post by:
This is not about starting a fight but an observation that seems to be proving itself on its own merit and is therefore simply a point of conjecture. I did not get serious about writing software...
6
by: Roger Heathcote | last post by:
sjdevnull@yahoo.com wrote: <snip> Fair point, but for sub processes that need to be in close contact with the original app, or very small functions that you'd like 100s or 1000s of it seems...
0
by: loorthu | last post by:
I am noticing that pexpect kills any child that it is spawned when the parent is terminated using SIGINT (e.g Ctrl-C on the shell), but not when it is killed by SIGKILL (e.g 'kill -9' on the parent...
9
MrPickle
by: MrPickle | last post by:
I have a structure like so: struct node { float x, y, z; std::vector<node> children; }; and I want to loop through all the node's children, then all the children's children then all the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.