My question is, after spawning a process, the child process is looping to get data from its stdin. I would like to write new data to it using either Echo.Process.pipes[0].write(data) or Echo.Process.writeToChild(0,data), but both do not work. Would someone explain what's going on? Or how do I go around this problem?
This is the error I got:
--- <exception caught here> ---
File "/usr/local/encap/python-2.6.4/lib/python2.6/site-packages/Twisted-9.0.0-py2.6-linux-x86_64.egg/twisted/internet/selectreactor.py", line 146, in _doReadOrWrite
why = getattr(selectable, method)()
File "/usr/local/encap/python-2.6.4/lib/python2.6/site-packages/Twisted-9.0.0-py2.6-linux-x86_64.egg/twisted/internet/tcp.py", line 460, in doRead
return self.protocol.dataReceived(data)
File "pp1.py", line 30, in dataReceived
Echo.Process.pipes[0].write(data)
exceptions.KeyError: 0
Thank You,
Q
from sys import executable
from os import environ
import os
from twisted.internet import reactor
from twisted.internet.protocol import Protocol
from twisted.internet.protocol import Factory
from twisted.internet import protocol
import sys
implementation = """\
import os
import time
import sys
print "in child", os.getpid()
while (True):
a = raw_input("")
if a: print a
"""
class Echo(Protocol):
Process = None
def dataReceived(self, data):
if Echo.Process == None:
pp = MyPP()
Echo.Process = reactor.spawnProcess(pp, executable, [executable, "-c", implementation, data], env=environ, childFDs = {0:1, 1:1, 2:2})
else:
Echo.Process.pipes[0].write(data)
#Echo.Process.writeToChild(0,data)
self.transport.write(data)
class EchoFactory(Factory):
def buildProtocol(self, addr):
return Echo()
class MyPP(protocol.ProcessProtocol):
def connectionMade(self):
print "connectionMade!"
def outReceived(self, data):
print "out"
def errReceived(self, data):
print "error", data
def processExited(self, reason):
print "processExited"
def processEnded(self, reason):
print "processEnded"
print "quitting"
reactor.listenTCP(8200, EchoFactory())
print 'in parent', os.getpid()
reactor.run()