Different behavior with multiprocessing

Tomas Kotal tomas.kotal at gmail.com
Mon Mar 4 15:31:41 CET 2013


Hi all.

Recently I discovered a strange behavior with multiprocessing library and call to function os.system (a different behavior under Linux and Windows to be more specific). I have this simple testing script:

############################################
import sys
import os
from multiprocessing import Process

def do_fork(cmd):
    ret = os.system(cmd)
    # print result of call
    print ret
    os._exit(ret)

if __name__ == "__main__":
    cmds = [ "dir", "xy" ]

    procs = []
    for cmd in cmds:
        proc = Process(target=do_fork, args=(cmd, ))
        proc.start()
        procs.append( proc )

    for proc in procs:
        proc.join()
        print "exitcode: %d" % proc.exitcode

    print "ok" 
############################################

This script just starts 2 processes. Each process executes one command in system shell and exits setting it's exit code same as exit code of a system call. There are 2 commands: "dir" (which works fine on Windows and Linux) and "xy" (which is supposed to fail under both systems). The strange thing is that when I run this script under Windows I get this output:

0
1
exitcode: 0
exitcode: 1
ok

The first 0 and 1 are results of os.system call which are printed from child processes. Rest of lines are printed from main process. This is expected output.

But when I run same script on Linux, what I get is this:

0
32512
exitcode: 0
exitcode: 0
ok

Although the second command fails and returns exit code 32512, the exit code from process in parent process gives me 0. I tried to change the script to use fork() instead of Process but the result was same.

Can anybody explain me what's the problem here?

(I use Python 2.6 on both Windows and Linux machines)



More information about the Python-list mailing list