Execute code after death of all child processes

Markus Franz mf1987 at arcor.de
Fri Dec 24 12:14:01 CET 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 env_sources[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