start function in new process

wongjoekmeu at yahoo.com wongjoekmeu at yahoo.com
Fri Mar 5 20:21:32 CET 2010


Hello all,

I would like to run a python function completely in a new process. For
example I have a parent process. That parent process needs to start a
child process in which the function is called. After the child process
is finished however I want that the child process should stop and then
only the parent process remain. When I start the function I want to
pass arguments also which include a function reference. I tried
os.fork() as I need to write it in linux. However it seems like the
child process remain alive. I discovered this when I loop through the
function. I see that the child process continue with the loop and
starts again a new grand child untill the loop is finished. See code
snippet. f() is the function I want to run in a new process. It got as
one of the input a refence to a function c().
Function g() I called in a loop for 3 times. Now it seems that when
child process get started in the function g() it return to the loop
and it calls g() itself and starts itself a nwe child process. I do
not want thant. I want the child process to perform the function f()
and just finished. Does anyone know how I can achieve this ?


def c():
    print "function c"

def f(a,b, d):
    # function I want to run in a new child process
    print a, b
    d()

def g():
    pidID = os.fork()
    if pidID == 0:
        # child do something here
        f(2,3,c)
    else:
        # parent do something here
        print "Parent

for i in range(3):
    g()






More information about the Python-list mailing list