How to get return values of a forked process

Ian ian.lake at rocketmail.com
Tue Jun 21 14:26:12 EDT 2011


Hello all,

I need some helped with forking.  In my script, I fork a process.  I
want to get return values from the child process.

This is the script that does the forking:
for x in (mylist):
   pid = os.fork()
   if pid:
         pidList.append(pid)
   else:
        os.execv('/usr/bin/python',('/usr/bin/
python',myForkedScript))

for pid in pidList:
        childPid, status = os.waitpid(pid,0)
        # I think status should be the return value of the forked
process; I would expect status to be a 1 or a 0

myForkedScript has code like this:
if fail:
   os._exit(1)
else:
   os._exit(os.EX_OK)


Is using os._exit() the correct way to get a return value back to the
main process?

I thought the value 'n', passed in os._exit(n) would be the value I
get returned.  In the case of a failure, I get 256 returned rather
than 1.

Thanks for the assistance!
IL



More information about the Python-list mailing list