Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > How to Spawn a process with P_NOWAIT and pass it some data ?

Reply
Thread Tools

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

 
 
Barak, Ron
Guest
Posts: n/a
 
      05-20-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()


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Re: How to Spawn a process with P_NOWAIT and pass it some data ? MRAB Python 0 05-20-2009 01:47 PM
Logging stdout/stderr/stdin of an spawn process (Open4::spawn) Edgardo Hames Ruby 1 05-06-2008 08:17 PM
Logging stdout/stderr/stdin of an spawn process (Open4::spawn) Ed Hames Ruby 0 04-16-2008 04:21 PM
spawn syntax + os.P_WAIT mode behavior + spawn stdout redirection Derek Basch Python 2 01-21-2005 05:37 AM



Advertisments