473,320 Members | 1,916 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.

Detecting problems in a forked process

Hey everyone. I'm writing a small application in Python that uses
os.fork() to create a separate process in which another application is
run in the background. The problem is that I need to know whether or
not that separate application managed to start and return from within
the parent appropriately. Here's, roughly, a pseudo example of what I
want to do (I know the code itself is not correct):

def function():

pid = os.fork()

if pid:
do parent stuff
else:
try:
execute the application
exeption:
somehow return -1 from within the parent process

#try to start another application in the background from within a forked
process
if (function() == -1):
fail

Is there a way that I can do this? I thought, only for a *VERY* brief
second, about cheating my way around this with a global variable, but
then I realized that this wouldn't work due to the fact that I will have
multiple forks doing the same thing at the same time. Thanks in advance :)

James

--
My blog: http://www.crazydrclaw.com/
My homepage: http://james.colannino.org/

"If Carpenters made houses the way programmers design programs, the first woodpecker to come along would destroy all of civilization." --Computer Proverb

Dec 29 '05 #1
2 1522
In article <ma***************************************@python. org>,
James Colannino <ja***@colannino.org> wrote:
Hey everyone. I'm writing a small application in Python that uses
os.fork() to create a separate process in which another application is
run in the background. The problem is that I need to know whether or
not that separate application managed to start and return from within
the parent appropriately. Here's, roughly, a pseudo example of what I
want to do (I know the code itself is not correct):

def function():

pid = os.fork()

if pid:
do parent stuff
else:
try:
execute the application
exeption:
somehow return -1 from within the parent process

#try to start another application in the background from within a forked
process
if (function() == -1):
fail

Is there a way that I can do this? I thought, only for a *VERY* brief
second, about cheating my way around this with a global variable, but
then I realized that this wouldn't work due to the fact that I will have
multiple forks doing the same thing at the same time. Thanks in advance :)


options:

Have the child set it's exit code to indicate success or failure and
use one of the various os.wait functions in the parent to retrieve it.

or

create a pipe before forking and you can pass data back and forth
between the parent and child

or

look at one of the shared memory libraries

--
Jim Segrave (je*@jes-2.demon.nl)

Dec 29 '05 #2
Quoth Jim Segrave <je*@nl.demon.net>:
| In article <ma***************************************@python. org>,
| James Colannino <ja***@colannino.org> wrote:
|> Hey everyone. I'm writing a small application in Python that uses
|> os.fork() to create a separate process in which another application is
|> run in the background. The problem is that I need to know whether or
|> not that separate application managed to start and return from within
|> the parent appropriately.
....
|> Is there a way that I can do this? I thought, only for a *VERY* brief
|> second, about cheating my way around this with a global variable, but
|> then I realized that this wouldn't work due to the fact that I will have
|> multiple forks doing the same thing at the same time. Thanks in advance :)

If your thought there had continued for another second or two, you
probably would have wondered whether variables are shared between
forks. (Actually they are, but only as an optimization - changes
are not shared.)

| options:
|
| Have the child set it's exit code to indicate success or failure and
| use one of the various os.wait functions in the parent to retrieve it.

This is the simplest, as long as it's easy to wait long enough to reliably
catch the failure case.

| create a pipe before forking and you can pass data back and forth
| between the parent and child

If you want the parent to wait for errors right up to the successful
execve(), and then continue, you can set the 'close on exec' flag on
the pipe write end file descriptor and make sure no process holds
this open but the child fork. See the subprocess module for example
code (or just use the subprocess module, if it's supported in the
deployed Python version.)

Donn Cave, do**@drizzle.com
Dec 30 '05 #3

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

Similar topics

21
by: John Lin | last post by:
Howdy, I want to know how to tell if a forked process is done. Actually, my real question is that I want to run a shell script inside of a python script, and after the shell script has...
2
by: RL | last post by:
Hello Perl gurus, 1. I have a web page where I can push a button (dospawn.html). 2. This button calls a CGI script (spawnboss.cgi) 3. spawnboss.cgi calls a forking perl script (forkme.pl) 4....
1
by: Alexander N. Spitzer | last post by:
I am trying to write a program that will fork a process, and execute the given task... the catch is that if it runs too long, I want to clean it up. this seemed pretty straight forward with a...
4
by: kmkz | last post by:
Hi, I have a program A that forks off two other programs, B and C. I need B and C to both terminate if A is closed, but by using the subprocess.call() method this seems to not be the case; I can...
1
by: vduber6er | last post by:
Hi I want to have a wait page while the rest of the cgi does its process, but it seems like the wait page waits till everything is complete and never appears. I've tried forking twice already as...
3
by: Simon | last post by:
Hi everyone, Can anyone tell me if there is a standard way of detecting an error in a process that I've started using the .net framework's System.Diagnostics.Process class? I need to...
13
by: Ilias Lazaridis | last post by:
How to detect memory leaks of python programms, which run in an environment like this: * Suse Linux 9.3 * Apache * mod_python The problem occoured after some updates on the infrastructure....
2
by: manontheedge | last post by:
I'm trying to write a program in C on Linux where the child processes get their process images replaced. I have in my code, a few child processes that I forked from a single parent process. I'm...
19
by: Ant | last post by:
Hi all, I have a question on PyParsing. I am trying to create a parser for a hierarchical todo list format, but have hit a stumbling block. I have parsers for the header of the list (title and...
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...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
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: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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.