[2.4.2/Linux] Getting Python to fork?

Gary Herron gherron at islandtraining.com
Mon Feb 4 00:54:58 EST 2008


Gilles Ganault wrote:
> Hello
>
> 	I need to launch a Python script, and fork it so that the calling
> script can resume with the next step will the Python script keeps
> running.
>
> I tried those two, but they don't work, as the calling script is stuck
> until the Python script ends:
>
> sys.stdout = open(os.devnull, 'w')
>
> =====
> #if os.fork():
> pid = os.fork()   
> if pid > 0:       
>         sys.exit(0)        
> =====
>
> Should I use another library to do this?
>
> Thank you.
>   
What OS?  Stuck how?   I you want both processes to execute, why do you
call sys.exit?  I don't think you've shown enough code to tell what you
are doing right or wrong.  Try this:

pid = os.fork()
if pid:
    # Original fork continues here
    # pid is child's process id
    # ... so onwards with the next step
    # If it needs to wait for the child to complete, it can call os.waitpid
else:
    # New fork continues here, independently of the original process
    # doing whatever the fork was create for

Gary Herron








More information about the Python-list mailing list