spawnv( ) or spawnl( ) do not launch a normal running process in Python 2.2.2?

N

nushin

Try to launch a test program that prints hello world for a minute or
so using, spawnv( ) or spawnl( ). Check to see the process state code
that the program is running. I am using RedHat Linux 7.3 and Python
2.2.2 and i see that the program is either launched as Zombie state
using spawnv(), or running in State "S" sleep in spawnl( ).

Here's the sample code that launches my program:

os.spawnv(os.P_NOWAIT,'/usr/bin/python',('python hello.py >/dev/null
&'))


os.spawnl(os.P_NOWAIT,'/usr/bin/python',('python hello.py >/dev/null
&'))

When you launch a program in Linux, you want it to run in state "R" ,
a running state. Am i the only one who has this problem? Would the
author of spawnv(), spawnl() look into this problem please.

Regards,
Nushin
 
P

Patrick Useldinger

nushin said:
os.spawnv(os.P_NOWAIT,'/usr/bin/python',('python hello.py >/dev/null
&'))


os.spawnl(os.P_NOWAIT,'/usr/bin/python',('python hello.py >/dev/null
&'))

Did you try *without* the redirections?
 
J

Jeff Epler

the third argument to os.spawnv is an argument list as in execv, not a
command string as in popen and system. The statement you listed
> os.spawnv(os.P_NOWAIT,'/usr/bin/python',('python hello.py >/dev/null
runs the Python binary with its argv[0] set to 'python hello.py >/dev/null',
which is probably going to drop into trying to read a script from
standard input since there is no script or command on the commandline,
but I'm not really sure what to expect in this crazy case.

There's no easy way to do command redirections while using spawnv.
Here's a piece of code to do so with fork+exec [tested]:
def F(x):
if not isinstance(x, int): return x.fileno()

def coroutine(args, child_stdin, child_stdout, child_stderr):
pid = os.fork()
if pid == 0:
os.dup2(F(child_stdin), 0)
os.dup2(F(child_stdout), 1)
os.dup2(F(child_stderr), 2)
os.execvp(args[0], args)
return pid
you could do something similar with os.spawnv [untested]:
def dup2_noerror(a, b):
try:
os.dup2(a, b)
except:
pass

def coroutine_spawnv(flags, args, child_stdin, child_stdout, child_stderr):
old_stdin = os.dup(0)
old_stdout = os.dup(1)
old_stderr = os.dup(2)
try:
os.dup2(F(child_stdin), 0)
os.dup2(F(child_stdout), 1)
os.dup2(F(child_stderr), 2)
return os.spawnv(flags, args[0], args)
finally:
dup2_noerror(old_stdin, 0)
dup2_noerror(old_stdout, 1)
dup2_noerror(old_stderr, 2)

Jeff
 
N

nushin

Thanks Jeff. Yes, i think it's the stdio buffering that causes
P_NOWAIT act asif it's a P_WAIT. I wish an additional parameter could
be added to spawnv( ) to toggle its stdout on/off.

Regards,
Nushin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top