Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > spawnv( ) or spawnl( ) do not launch a normal running process in Python 2.2.2?

Reply
Thread Tools

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

 
 
nushin
Guest
Posts: n/a
 
      08-04-2003
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
 
Reply With Quote
 
 
 
 
Patrick Useldinger
Guest
Posts: n/a
 
      08-04-2003
nushin wrote:

> 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?

--
Real e-mail address is 'cHVAdm8ubHU=\n'.decode('base64')
Visit my Homepage at http://www.homepages.lu/pu/

 
Reply With Quote
 
 
 
 
Jeff Epler
Guest
Posts: n/a
 
      08-05-2003
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

 
Reply With Quote
 
nushin
Guest
Posts: n/a
 
      08-07-2003
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

Jeff Epler <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> I don't see any problem with P_NOWAIT. Take the following for example:
>
> $ cat nushin.py
> import os
>
> p = os.spawnvp(os.P_NOWAIT, 'sh',
> ['sh', '-c', 'sleep 1; echo from spawnv'])
> print "from program"
> print "waitpid returns:", os.waitpid(p, 0)
> $ python -u nushin.py
> from program
> waitpid returns:from spawnv
> (2826, 0)
>
> Now, if the program completed very quickly, it's a coin-flip whether its
> output would appear before "from program". In this case, I made sure
> the program would take a really long time (1 second) to complete.
>
> When running without -u but not on a terminal, you might see
> $ python nushin.py | cat
> from spawnv
> from program
> waitpid returns: (2832, 0)
> .. this is because the Python process has printed "from program", but
> stdio buffering has kept it from actually being written to the output
> yet.
>
> Here's what you see on a terminal without -u:
> $ python nushin.py
> from program
> from spawnv
> waitpid returns: (2835, 0)
> This is the same thing you'd see if the program said
> print "from program"; sys.stdout.flush()
>
> Jeff

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Spawn a process, then exit, whilst leaving process running? Victor Hooi Python 1 02-10-2013 03:50 AM
Is Display.dll*32 a normal process for XP64? Peter de B. Harrington Windows 64bit 2 06-20-2006 07:37 PM
Process.destroy() -- process carries on running? John English Java 0 11-07-2005 05:38 PM
Show up process indicator while process is running then do page redirection Jona ASP .Net 2 09-01-2005 04:03 AM
Problem destroying Process after reading normal and error output. molar Java 0 07-25-2004 09:54 PM



Advertisments