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

Discussion in 'Python' started by Barak, Ron, May 20, 2009.

  1. Barak, Ron

    Barak, Ron Guest

    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()
    Barak, Ron, May 20, 2009
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Derek Basch
    Replies:
    2
    Views:
    1,283
    Donn Cave
    Jan 21, 2005
  2. MRAB
    Replies:
    0
    Views:
    845
  3. Ed Hames
    Replies:
    0
    Views:
    362
    Ed Hames
    Apr 16, 2008
  4. Edgardo Hames
    Replies:
    1
    Views:
    332
    Ed Hames
    May 6, 2008
  5. Victor Hooi
    Replies:
    1
    Views:
    129
    Nobody
    Feb 10, 2013
Loading...

Share This Page