the windows equivalent of fork()?

Bjorn Pettersen bpetterson at uswest.net
Sun Mar 19 02:05:25 CET 2000


The constants are in the os module, so os.P_WAIT instead of just plain
P_WAIT should solve your error.

-- bjorn

DataJockey wrote:

> I am looking for a way to use the following functionallity in windows.
> #test.py
> import os
>
> def spawn(prog, *args):
>     pipe1 = os.pipe()
>     pipe2 = os.pipe()
>     pid = os.fork()
>     if pid:
>         os.close(pipe1[1])
>         os.close(pipe2[0])
>         os.dup2(pipe1[0], 0)
>         os.dup2(pipe2[1], 1)
>
>     else:
>         os.close(pipe1[0])
>         os.close(pipe2[1])
>         os.dup2(pipe2[0], 0)
>         os.dup2(pipe1[1], 1)
>         args = (prog,) + args
>         os.execv(prog, args)
>
> spawn('C:\WINDOWS\Cdplayer.exe')
>
> if is run this i get an 'AttributeError: fork'  that i understand,
> fork() is a unix specific call.  however i do not know how to achieve
> the same functionallity with windows.
>
> i reference http://www.python.org/doc/current/lib/os-process.html and i
> see
>
> spawnv (mode, path, args)
> with constants for mode:
>     P_WAIT
>     P_NOWAIT
>     P_NOWAITO
>     P_OVERLAY
>     P_DETACH
> however if i try this
>         os.spawnv(P_WAIT, 'C:\WINDOWS\Cdplayer.exe')
>     the result is a NameError: P_WAIT
>         os.spawnv('P_WAIT', 'C:\WINDOWS\Cdplayer.exe')
>     the result is TypeError: 3-sequence, 2-sequence
>             i do not know what this means, sorry
>
> system (command)
>         if i try os.system('os.execl("C:\WINDOWS\Cdplayer.exe")')
>     a shell spawns and then exits, i get a result of 0 in the
> interpretor
>
> in my books i have found that there is a command os.popen() should
> automatically do the same as the above code spawn() from test.py.  i can
> not find popen()'s defenition in the modules.  there is a popen2.py, but
> the code in this file uses fork(), so i am assuming that the fork() call
> is causing the following errors.
>
> if i do os.popen('') to spawn a shell, the result
>     OSError: (0, 'Error')
> if i do os.popen('os.execl("C:\WINDOWS\Cdplayer.exe")')
>     OSError: (0, 'Error')
>
> and finally if i just do os.execl("C:\WINDOWS\Cdplayer.exe")
> python will preform an illegal op and exit, but it will open the cd
> player.
>
> i hope someone can help :)
> i am new to python and i love it.
> i have learned a lot by playing with python, and i can usually figure
> out my problems. but this one i can not, please help.
>
> if you read this far, you rock!
>
> thanks in advance
> jens page




More information about the Python-list mailing list