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()

<<inline: attcb52c.jpg>>

<<inline: attcb54c.jpg>>

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to