Execute code after death of all child processes

M

Markus Franz

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
 
M

Mike Meyer

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.

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

Basically, make sure that none of the children run the waitpid and
last print.

<mike
 
T

Terry Reedy

I am a little puzzled about your variable declaration and use:
import sys, os, time
texts = ['this is text1', 'this is text 2']

You don't seem to use 'texts',
for current_text in env_sources[0:]:

or define 'env_sources',
pid = os.fork()
if pid == 0:
time.sleep(2)
print current_text
break

so I don't see how this could run (even with indents inserted back). What
am I missing?

Terry J. Reedy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top