473,320 Members | 1,845 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,320 software developers and data experts.

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

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
Jul 18 '05 #1
1 1104
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-----

Jul 18 '05 #2

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

Similar topics

2
by: Rech | last post by:
Hi, I need a little help here managing child processes in Python. I'm not so skilled in system programming so I hope you can give me some good suggestions. I have a very CPU and memory...
2
by: Markus Franz | last post by:
Hallo! I have a problem with this little script written in Python: import sys, os, time texts = for current_text in env_sources: pid = os.fork()
1
by: dbb9h | last post by:
I've noticed a problem with calls to server.execute on a webserver running IIS 5.0. The call is used to conditionally include some asp code according to the language, but there seem to exist...
1
by: Thomas | last post by:
Hi all, I want to execute a new program in a c++ program without beeing blocked. After the child process is created both processes should run idependend. My method at the moment: execute(...
23
by: Jeff Rodriguez | last post by:
Here's what I want do: Have a main daemon which starts up several threads in a Boss-Queue structure. From those threads, I want them all to sit and watch a queue. Once an entry goes into the...
7
by: msxkim | last post by:
How to execute functions in the parent class first and then functions in the child class? For example, I have a parent class with functions 'ONE' and 'TWO' and child class has a function 'THREE'. ...
2
by: revenant81 | last post by:
I'm writing a program which has to execute a command, get its output and show it on a treeview. This command runs for a very long time. I want to end the execution of the command when the user...
22
by: Jason Zheng | last post by:
This may be a silly question but is possible for os.wait() to lose track of child processes? I'm running Python 2.4.4 on Linux kernel 2.6.20 (i686), gcc4.1.1, and glibc-2.5. Here's what happened...
2
by: rocco.rossi | last post by:
I'm using the Python processing module. I've just run into a problem though. Actually, it's a more general problem that isn't specific to this module, but to the handling of Unix (Linux processes)...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
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...
0
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...
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: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.