Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Execute code after death of all child processes - (corrected posting)

Reply
Thread Tools

Execute code after death of all child processes - (corrected posting)

 
 
Markus Franz
Guest
Posts: n/a
 
      12-25-2004
Hallo!


I have a problem with this little script written in Python:


import sys, os, time
texts = ['this is text1', 'this is text 2']
for current_text in env_sources[0:]:
pid = os.fork()
if pid == 0:
time.sleep(2)
print current_text
break

As you is create some child processes depending on the number of vars
in ?texts?. My problem is: How can I run some code after all child
processes have been finished? (The code should be run only in the
parent process.)

I tried:

import sys, os, time
texts = ['this is text1', 'this is text 2']
for current_text in texts[0:]:
pid = os.fork()
if pid == 0:
time.sleep(2)
print current_text
break
os.waitpid(-1, 0)
print 'this is the end'

But this didn't work, 'this is the end' will be showed several times.

Can you help me?

Tanks.


Markus
 
Reply With Quote
 
 
 
 
Jeff Epler
Guest
Posts: n/a
 
      12-25-2004
First, you'll want to exit from each forked copy, or else it will reach
the code-after-the-for-loop:
import sys, os, time
texts = ['this is text1', 'this is text 2']
for current_text in texts[0:]:
pid = os.fork()
if pid == 0:
time.sleep(2)
print current_text
raise SystemExit

Next, you'll want to wait for each process you started:
for current_text in texts:
os.waitpid(-1, 0)
print 'this is the end'


$ python /tmp/franz.py
this is text1
this is text 2
this is the end

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)

iD8DBQFBzXcmJd01MZaTXX0RAjtXAKCV0GZ7o3GW3bJ0pBRdgH +CyLcjAgCeJ2cO
Op+TNBqmhANGSEhIUYAq6eE=
=O4tl
-----END PGP SIGNATURE-----

 
Reply With Quote
 
 
 
 
Denis S. Otkidach
Guest
Posts: n/a
 
      12-28-2004
On Sat, 25 Dec 2004 08:20:24 -0600
Jeff Epler <> wrote:

> Next, you'll want to wait for each process you started:
> for current_text in texts:
> os.waitpid(-1, 0)
> print 'this is the end'


This can eventually die with exception. The proper way is:

try:
while True:
os.waitpid(-1, 0)
except OSError, exc:
if exc.errno!=errno.ECHILD:
raise

--
Denis S. Otkidach
http://www.python.ru/ [ru]
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Death, even when expected, is hard for anyone to deal with, but asuicidal death asim malik Perl Misc 1 09-13-2009 07:42 AM
Child processes live after parent process is killed Uwe Kubosch Ruby 5 02-02-2008 05:20 AM
Re: Crocodile Hunter - DEATH VIDEO - Steve Irwin - crocodile hunter - death video - steve irwin.exe (1/1) Mikey DVD Video 3 09-14-2006 05:52 PM
Execute code after death of all child processes Markus Franz Python 2 12-24-2004 06:11 PM
How do I: Main thread spawn child threads, which child processes...control those child processes? Jeff Rodriguez C Programming 23 12-09-2003 11:06 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57