When I use os.popen3 to communicate through pipes under Windows, the
data is only one-way, namely from child to parent. Below are example
scripts. Is this a bug in the Windows implementation or am I missing
something?
Any help is appreciated.
#----------------------------------
# controller.py
import threading
import sys
import time
import os
(w,r,e) = os.popen3('engine.py')
def receive(r):
print 'staring recv'
while 1:
# get data
try:
data = r.readline()
except Exception, e:
print e
break
if not len(data):
time.sleep(0.1)
continue
else:
print 'controller received:', data
print 'stopping recv'
t = threading.Thread(target=receive, args=(r,))
t.setDaemon(True)
t.start()
for i in range(5):
time.sleep(1)
if not w.closed:
print >>w, 'controller '+str(i)
w.flush()
print 'final:', ''.join(r.readlines())
print 'errors:', ''.join(e.readlines())
r.close()
w.close()
e.close()
#-------------------------
# engine.py
import sys
import threading
import time
outfile = open('enginetest.data', 'w')
def receive():
global outfile
print >>outfile, 'starting recv'
while 1:
# get data
try:
data = sys.stdin.readline()
except Exception, e:
print >>outfile, 'error:', e
break
if not len(data):
time.sleep(0.1)
continue
else:
print >>outfile, data
print >>outfile, data
print >>outfile, 'stopping recv'
t = threading.Thread(target=receive)
t.setDaemon(True)
t.start()
for i in range(4):
time.sleep(1)
if not sys.stdout.closed:
print >>sys.stdout, 'engine',str(i)
sys.stdout.flush()
outfile.close()