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

Jeff Epler jepler at unpythonic.net
Thu Aug 7 21:55:15 EDT 2003


On Thu, Aug 07, 2003 at 03:39:34PM -0700, nushin wrote:
> 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.

I'm not sure what you're asking for.  An earlier message I sent to you
used dup2 + spwanv to redirect a child's standard input, output, and
error.  Using /dev/null here should be a way to turn the child's stdout
off.

You can even beef it up a little bit by flushing the main program's
standard files before spawning, and letting an argument of 'None'
open /dev/null

Of course, all this is still untested...

    F_devnull = None
    def F(x):
	"""Return the file number corresponding to the given object
F(x) -> fd corresponding to /dev/null if x is None
F(x) -> x  if isinstance(x, int)
F(x) -> x.fileno() otherwise
"""      
	if f is None:
	    global F_devnull
            if F_devnull is None:
                F_devnull = os.open("/dev/null", os.O_RDWR)
            return F_devnull
        if not isinstance(x, int): return x.fileno()

    def dup2_noerror(a, b):
	"a dup2 that suppresses all errors.  For use in cleanup"
        try:
            os.dup2(a, b)
        except:
            pass

    def coroutine_spawnv(flags, args, child_stdin, child_stdout, child_stderr):
	"Call spawnv with a different set of files for stdin/stdio/stderr"
	sys.stdout.flush(); sys.stderr.flush()
        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)


> 
> Regards,
> Nushin
> 
> Jeff Epler <jepler at unpythonic.net> wrote in message news:<mailman.1060257314.28503.python-list at python.org>...
> > 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
> -- 
> http://mail.python.org/mailman/listinfo/python-list
> 





More information about the Python-list mailing list