Re: How to get return values of a forked process



On Tue, Jun 21, 2011 at 12:26 PM, Ian <ian.lake@xxxxxxxxxxxxxx> wrote:
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?

sys.exit() is the preferred way.

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.

According to the docs, on Unix:

"""
Wait for completion of a child process, and return a tuple containing
its pid and exit status indication: a 16-bit number, whose low byte is
the signal number that killed the process, and whose high byte is the
exit status (if the signal number is zero); the high bit of the low
byte is set if a core file was produced.
"""

And on Windows:

"""
Wait for completion of a process given by process handle pid, and
return a tuple containing pid, and its exit status shifted left by 8
bits (shifting makes cross-platform use of the function easier).
"""

(256 >> 8) == 1

However, I would advise using the subprocess module for this instead
of the os module (which is just low-level wrappers around system
calls).
.



Relevant Pages

  • Re: How to get return values of a forked process
    ... its pid and exit status indication: a 16-bit number, ... I would advise using the subprocess module for this instead ... Where did you find the Unix docs you pasted in? ...
    (comp.lang.python)
  • Postfix - Help on configuration
    ... I want configure Postfix on my server. ... [root@dummysites jlvill]# mail jlvill ... /usr/lib/postfix/local pid 8184 exit status 1 ...
    (comp.os.linux.setup)
  • Re: fork defunct process, already forked twice
    ... Is there thing else I have to close off before the child dies like ... if (pid> 0){ ... // parent waits on child to exit ... if (gpid < 0){ ...
    (comp.unix.questions)
  • Re: single run
    ... and show warning like "Program already running" and exit. ... and print to it the PID of the program. ... I'm not sure how this is done on windows, but on *nix, you can use the 'ps' command, and grep for the PID. ... If you found it, then exit, else replace the PID inside your file, and continue running. ...
    (comp.lang.perl.tk)
  • subprocess returncode is masked
    ... I'm using Python 2.6 and the new subprocess module to get the exit value of an external executable. ... It appears the return value given by waitor polloperations is masked under Unix: I only get the lower 8 bits. ...
    (comp.lang.python)