<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=windows-1255">
<META content="MSHTML 6.00.2900.5764" name=GENERATOR></HEAD>
<BODY>
<DIV><FONT face=Arial color=#0000ff><SPAN
class=177305311-20052009>Hi,</SPAN></FONT></DIV>
<DIV><FONT face=Arial color=#0000ff><SPAN
class=177305311-20052009></SPAN></FONT> </DIV>
<DIV><FONT face=Arial color=#0000ff><SPAN class=177305311-20052009>This is my
first try at IPC in Python, and I would like to ask your help with the following
problem:</SPAN></FONT></DIV>
<DIV><FONT face=Arial color=#0000ff><SPAN
class=177305311-20052009></SPAN></FONT> </DIV>
<DIV><FONT face="Arial Unicode MS" color=#0000ff size=4><SPAN
class=177305311-20052009>I would like to spawn a process with P_NOWAIT, and pass
some data to the child process.</SPAN></FONT></DIV>
<DIV><FONT face=Arial color=#0000ff><SPAN
class=177305311-20052009></SPAN></FONT> </DIV>
<DIV><FONT face=Arial color=#0000ff><SPAN class=177305311-20052009>I created two
scripts to try IPC (in a blocking way):</SPAN></FONT></DIV>
<DIV><FONT face=Arial color=#0000ff><SPAN
class=177305311-20052009></SPAN></FONT> </DIV>
<DIV><FONT face=Courier><SPAN class=177305311-20052009>$ cat
subprocess_sender.py<BR>#!/usr/bin/env python</SPAN></FONT></DIV>
<DIV><FONT face=Courier></FONT> </DIV>
<DIV><FONT face=Courier><SPAN class=177305311-20052009>import
subprocess</SPAN></FONT></DIV>
<DIV><FONT face=Courier></FONT> </DIV>
<DIV><FONT face=Courier><SPAN class=177305311-20052009>proc =
subprocess.Popen(["python", "-u", "subprocess_receiver.py"],
stdin=subprocess.PIPE, shell=True)</SPAN></FONT></DIV>
<DIV><FONT face=Courier></FONT> </DIV>
<DIV><FONT face=Courier><SPAN
class=177305311-20052009>proc.communicate(input="this is sent from
subprocess_sender.py")[0]<BR>proc.stdin.close()<BR></SPAN></FONT></DIV>
<DIV><FONT face=Arial color=#0000ff><SPAN
class=177305311-20052009>and</SPAN></FONT></DIV>
<DIV><FONT face=Arial color=#0000ff><SPAN
class=177305311-20052009></SPAN></FONT> </DIV>
<DIV><FONT face=Courier><SPAN class=177305311-20052009>$ cat
subprocess_receiver.py <BR>#!/usr/bin/env python</SPAN></FONT></DIV>
<DIV><FONT face=Courier></FONT> </DIV>
<DIV><FONT face=Courier><SPAN class=177305311-20052009>import
sys</SPAN></FONT></DIV>
<DIV><FONT face=Courier></FONT> </DIV>
<DIV><FONT face=Courier><SPAN class=177305311-20052009>print
sys.stdin.readline()<BR></SPAN></FONT></DIV>
<DIV><FONT face=Arial color=#0000ff><SPAN class=177305311-20052009>These scripts
intercommunicate nicely:</SPAN></FONT></DIV>
<DIV><FONT face=Arial color=#0000ff><SPAN
class=177305311-20052009></SPAN></FONT> </DIV>
<DIV><FONT face=Courier><SPAN class=177305311-20052009>$ python -u
subprocess_sender.py<BR>this is sent from
subprocess_sender.py<BR></SPAN></FONT></DIV>
<DIV><FONT face=Arial color=#0000ff><SPAN class=177305311-20052009>The example
in the documentation is:</SPAN></FONT></DIV>
<DIV><FONT face=Arial color=#0000ff><SPAN class=177305311-20052009><SPAN
id=highlight_tag
style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; FONT-WEIGHT: bold; PADDING-BOTTOM: 0px; COLOR: red; PADDING-TOP: 0px; BACKGROUND-COLOR: #ffeeee; red: ">
<H3>18.1.3.4. Replacing the os.spawn family<A class=headerlink
title="Permalink to this headline"
href="http://docs.python.org/library/subprocess.html#replacing-the-os-spawn-family">¶</A></H3>
<P>P_NOWAIT example:</P>
<DIV class=highlight-python><PRE>pid = os.spawnlp(os.P_NOWAIT, "/bin/mycmd", "mycmd", "myarg")
==>
pid = Popen(["/bin/mycmd", "myarg"]).pid</PRE></DIV></SPAN></SPAN></FONT><FONT
face=Arial color=#0000ff></FONT></DIV>
<DIV><SPAN class=177305311-20052009><FONT face=Arial color=#0000ff>But, when I
try it in my script:</FONT></SPAN></DIV>
<DIV><SPAN class=177305311-20052009><FONT
face=Courier></FONT></SPAN> </DIV>
<DIV><FONT face=Courier>$ cat subprocess_sender.py<BR>#!/usr/bin/env
python</FONT></DIV>
<DIV><FONT face=Courier></FONT> </DIV>
<DIV><FONT face=Courier>import subprocess</FONT></DIV>
<DIV><FONT face=Courier></FONT> </DIV>
<DIV><FONT face=Courier>proc = subprocess.Popen(["python", "-u",
"subprocess_receiver.py"], stdin=subprocess.PIPE,
shell=True).<STRONG>pid</STRONG></FONT></DIV>
<DIV><FONT face=Courier></FONT> </DIV>
<DIV><FONT face=Courier>proc.communicate(input="this is sent from
subprocess_sender.py")[0]<BR>proc.stdin.close()<BR></FONT></DIV>
<DIV><SPAN class=177305311-20052009><FONT face=Arial color=#0000ff>proc is now
changed from a subprocess.Popen object to an int, namely:</FONT></SPAN></DIV>
<DIV><SPAN class=177305311-20052009><FONT face=Arial
color=#0000ff></FONT></SPAN> </DIV>
<DIV><SPAN class=177305311-20052009><FONT face=Courier>$ python -u
subprocess_sender.py<BR>Traceback (most recent call last):<BR> File
"subprocess_sender.py", line 7, in <module><BR>
proc.communicate(input="this is sent from
subprocess_sender.py")[0]<BR>AttributeError: 'int' object has no attribute
'communicate'<BR></FONT></SPAN></DIV>
<DIV><SPAN class=177305311-20052009><FONT face=Arial color=#0000ff>Can anyone
suggest what is the correct way to implement P_NOWAIT and still be able to
communicate with the child process ?</FONT></SPAN></DIV>
<DIV><SPAN class=177305311-20052009><FONT face=Arial color=#0000ff>(Or, is there
a way to create a subprocess.Popen object from what I assume is
the process handle integer ?)</FONT></SPAN></DIV>
<DIV><SPAN class=177305311-20052009><FONT face=Arial
color=#0000ff></FONT></SPAN> </DIV>
<DIV><SPAN class=177305311-20052009><FONT face=Arial
color=#0000ff>Thanks,</FONT></SPAN></DIV>
<DIV><SPAN class=177305311-20052009><FONT face=Arial
color=#0000ff>Ron.</DIV></FONT></SPAN>
<DIV><SPAN class=177305311-20052009><FONT face=Arial
color=#0000ff></FONT></SPAN> </DIV>
<DIV><SPAN class=177305311-20052009><FONT face=Arial color=#0000ff><U>The
numbering of the scripts' lines</U>:</FONT></SPAN></DIV>
<DIV><SPAN class=177305311-20052009><FONT face=Arial
color=#0000ff></FONT></SPAN> </DIV>
<DIV><SPAN class=177305311-20052009><FONT face=Courier>$ cat -n
subprocess_sender.py<BR> 1 #!/usr/bin/env
python<BR> 2<BR> 3 import
subprocess<BR> 4<BR> 5
proc = subprocess.Popen(["python", "-u", "subprocess_receiver.py"],
stdin=subprocess.PIPE, shell=True).pid<BR>
6<BR> 7 proc.communicate(input="this is sent from
subprocess_sender.py")[0]<BR> 8
proc.stdin.close()</FONT></SPAN></DIV>
<DIV><FONT face=Courier></FONT> </DIV>
<DIV><SPAN class=177305311-20052009><FONT face=Arial color=#0000ff><FONT
face=Courier color=#000000>$ cat -n subprocess_receiver.py
<BR> 1 #!/usr/bin/env
python<BR> 2<BR> 3 import
sys<BR> 4<BR> 5 print
sys.stdin.readline()<BR></FONT></DIV></FONT></SPAN>
<DIV><SPAN class=177305311-20052009><FONT face=Arial
color=#0000ff> </DIV></FONT></SPAN></BODY></HTML>