Bug in os.spawnv with P_NOWAIT

nushin nushin2 at yahoo.com
Fri Aug 8 00:45:08 CEST 2003

nushin2 at yahoo.com (nushin) wrote in message 
> If you do not null out the output of your parent process that launches
> a child process by os.spawnv( ) in P_NOWAIT mode, you will see that
> your child process is launched *synchronously* instead of
> asynchronously. This is a bug!
> Here's a code sample:
> In your parent.py file, call child.py as
> os.spawnv(os.P_NOWAIT,'/usr/bin/python',('python', 'child.py'))
> then, make sure your child.py prints a *hello child* to screen about
> 2000000 times, and your parent.py prints a *hello parent* message,
> then execute your file in a shell as:
> python parent.py
> then, you would see that whenever your child.py is finished, then
> parent.py finishes, i.e., a synchronous call.
> Now, null out the output of your parent process as:
> python parent.py > /dev/null
> then, your parent.py finishes first while child.py is still running,
> i.e., an asynchronous call. Please correct me if i am wrong.
> Regards,
> Nushin
Jeff Epler has shed some light on this issue. Thanks to him.
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

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.


More information about the Python-list mailing list