How to Spawn a process with P_NOWAIT and pass it some data ?
Barak, Ron
Ron.Barak at lsi.com
Wed May 20 08:31:03 EDT 2009
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
But, when I try it in my script:
$ cat subprocess_sender.py
#!/usr/bin/env python
import subprocess
proc = subprocess.Popen(["python", "-u", "subprocess_receiver.py"], stdin=subprocess.PIPE, shell=True).pid
proc.communicate(input="this is sent from subprocess_sender.py")[0]
proc.stdin.close()
proc is now changed from a subprocess.Popen object to an int, namely:
$ python -u subprocess_sender.py
Traceback (most recent call last):
File "subprocess_sender.py", line 7, in <module>
proc.communicate(input="this is sent from subprocess_sender.py")[0]
AttributeError: 'int' object has no attribute 'communicate'
Can anyone suggest what is the correct way to implement P_NOWAIT and still be able to communicate with the child process ?
(Or, is there a way to create a subprocess.Popen object from what I assume is the process handle integer ?)
Thanks,
Ron.
The numbering of the scripts' lines:
$ cat -n subprocess_sender.py
1 #!/usr/bin/env python
2
3 import subprocess
4
5 proc = subprocess.Popen(["python", "-u", "subprocess_receiver.py"], stdin=subprocess.PIPE, shell=True).pid
6
7 proc.communicate(input="this is sent from subprocess_sender.py")[0]
8 proc.stdin.close()
$ cat -n subprocess_receiver.py
1 #!/usr/bin/env python
2
3 import sys
4
5 print sys.stdin.readline()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090520/f32c45d5/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: attcb52c.jpg
Type: image/jpeg
Size: 1512 bytes
Desc: attcb52c.jpg
URL: <http://mail.python.org/pipermail/python-list/attachments/20090520/f32c45d5/attachment.jpg>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: attcb54c.jpg
Type: image/jpeg
Size: 1650 bytes
Desc: attcb54c.jpg
URL: <http://mail.python.org/pipermail/python-list/attachments/20090520/f32c45d5/attachment-0001.jpg>
More information about the Python-list
mailing list