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

Markus Franz mf1987 at arcor.de
Sat Dec 25 04:56:27 EST 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



More information about the Python-list mailing list