the windows equivalent of fork()? 
Author Message
 the windows equivalent of fork()?

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.*-*-*.com/
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



Wed, 04 Sep 2002 03:00:00 GMT  
 the windows equivalent of fork()?
The constants are in the os module, so os.P_WAIT instead of just plain
P_WAIT should solve your error.

-- bjorn

Quote:

> 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



Wed, 04 Sep 2002 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. nt equivalent of fork() ?

2. An equivalent to fork() on NT ?

3. Resource Fork has no Resource Fork?

4. fork, exec and wait on WINDOWS?

5. fork and duplicate window manager connections

6. Posible solution for Forks and Multiple Windows.

7. Windows equivalent of TSO stuff

8. Windows bitbucket equivalent?

9. libPython.a equivalent for MS-Windows? (fwd)

10. libPython.a equivalent for MS-Windows?

11. equivalent to exec(in UNIX) under Windows

12. LD_LIBRARY_PATH equivalent for windows?

 

 
Powered by phpBB® Forum Software