J
Jeremy Sanders
Hi - I have some code which works under linux. It starts a remote python
process using subprocess and communicates to it via a pipe created by
os.pipe. As far as I understand, child processes should inherit file
descriptors from the parent if close_fds=False on the suprocess.Popen
command line.
This code doesn't work under Window, but gives "bad file descriptor" when
trying to read from the pipe in the child process. I have some example code
which fails. It consists of two files, master.py and slave.py. The file
descriptor for reading from the pipe is passed as a argument to slave.py.
----------------------------------------------------------------
#!/usr/bin/env python
# This is master.py
import os
import os.path
import sys
import subprocess
def runMaster():
# create pipe to communicate with remote process
rpipe, wpipe = os.pipe()
# start remote process
cmdline = [sys.executable,
os.path.join( os.path.dirname(
os.path.abspath(__file__)), 'slave.py' ),
str(rpipe) ]
remote = subprocess.Popen(cmdline, shell=False, bufsize=0,
close_fds=False)
# send text to remote process via pipe
os.write(wpipe, 'hi there$')
# wait until remote exit
remote.wait()
if __name__ == '__main__':
runMaster()
------------------------------------------------------------------
# This is slave.py
import sys
import os
def runSlave(fd):
"""Copy text to stderr from file descriptor until a $ symbol."""
while True:
intext = os.read(fd, 1)
if intext == '$':
break
elif intext:
# write text from pipe to stderr
sys.stderr.write('* %s\n' % intext)
if __name__ == '__main__':
fd = int(sys.argv[1])
runSlave(fd)
-------------------------------------------------------------------
Does anyone have any ideas how to get this to work under Windows? Is it
correct code under unix?
Thanks
Jeremy
process using subprocess and communicates to it via a pipe created by
os.pipe. As far as I understand, child processes should inherit file
descriptors from the parent if close_fds=False on the suprocess.Popen
command line.
This code doesn't work under Window, but gives "bad file descriptor" when
trying to read from the pipe in the child process. I have some example code
which fails. It consists of two files, master.py and slave.py. The file
descriptor for reading from the pipe is passed as a argument to slave.py.
----------------------------------------------------------------
#!/usr/bin/env python
# This is master.py
import os
import os.path
import sys
import subprocess
def runMaster():
# create pipe to communicate with remote process
rpipe, wpipe = os.pipe()
# start remote process
cmdline = [sys.executable,
os.path.join( os.path.dirname(
os.path.abspath(__file__)), 'slave.py' ),
str(rpipe) ]
remote = subprocess.Popen(cmdline, shell=False, bufsize=0,
close_fds=False)
# send text to remote process via pipe
os.write(wpipe, 'hi there$')
# wait until remote exit
remote.wait()
if __name__ == '__main__':
runMaster()
------------------------------------------------------------------
# This is slave.py
import sys
import os
def runSlave(fd):
"""Copy text to stderr from file descriptor until a $ symbol."""
while True:
intext = os.read(fd, 1)
if intext == '$':
break
elif intext:
# write text from pipe to stderr
sys.stderr.write('* %s\n' % intext)
if __name__ == '__main__':
fd = int(sys.argv[1])
runSlave(fd)
-------------------------------------------------------------------
Does anyone have any ideas how to get this to work under Windows? Is it
correct code under unix?
Thanks
Jeremy