How to Spawn a process with P_NOWAIT and pass it some data ?

MRAB google at mrabarnett.plus.com
Wed May 20 09:47:03 EDT 2009


Barak, Ron wrote:
> Hi,
>  
> This is my first try at IPC in Python, and I would like to ask your help 
> with the following problem:
>  
> I would like to spawn a process with P_NOWAIT, and pass some data to the 
> child process.
>  
> I created two scripts to try IPC (in a blocking way):
>  
> $ cat subprocess_sender.py
> #!/usr/bin/env python
>  
> import subprocess
>  
> proc = subprocess.Popen(["python", "-u", "subprocess_receiver.py"], 
> stdin=subprocess.PIPE, shell=True)
>  
> proc.communicate(input="this is sent from subprocess_sender.py")[0]
> proc.stdin.close()
> and
>  
> $ cat subprocess_receiver.py
> #!/usr/bin/env python
>  
> import sys
>  
> print sys.stdin.readline()
> These scripts intercommunicate nicely:
>  
> $ python -u  subprocess_sender.py
> this is sent from subprocess_sender.py
> The example in the documentation is:
> 
> 
>       18.1.3.4. Replacing the os.spawn family¶
>       <http://docs.python.org/library/subprocess.html#replacing-the-os-spawn-family>
> 
> P_NOWAIT example:
> 
> pid = os.spawnlp(os.P_NOWAIT, "/bin/mycmd", "mycmd", "myarg")
> ==>
> pid = Popen(["/bin/mycmd", "myarg"]).pid
> 
[snip]
The documentation is showing the 'subprocess' equivalents of older calls
(the 'subprocess' module was added in Python 2.4).

This means is that "Popen" is equivalent to calling "spawnlp" with
P_NOWAIT.



More information about the Python-list mailing list