Twisted Python: Cannot Write To A Running Spawned Process
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(dat
Solution 1:
To create a new process on each incoming connection and to redirect all input data to the process' stdin:
#!/usr/bin/pythonfrom twisted.internet import reactor
from twisted.internet import protocol
classEcho(protocol.Protocol):
defconnectionMade(self):
self.pp = MyPP()
reactor.spawnProcess(self.pp, 'cat', ['cat'])
defdataReceived(self, data):
self.pp.transport.write(data)
defconnectionLost(self, reason):
self.pp.transport.loseConnection()
classMyPP(protocol.ProcessProtocol):
defconnectionMade(self):
print"connectionMade!"defoutReceived(self, data):
print"out", data,
deferrReceived(self, data):
print"error", data,
defprocessExited(self, reason):
print"processExited"defprocessEnded(self, reason):
print"processEnded"print"quitting"
factory = protocol.Factory()
factory.protocol = Echo
reactor.listenTCP(8200, factory)
reactor.run()
Solution 2:
Don't pass childFDs
to spawnProcess
and don't use the pipes
attribute of the resulting process transport object. Neither of these things does what you think. If you drop the use of childFDs
and switch back to writeToChild
, you'll get the behavior you want.
Post a Comment for "Twisted Python: Cannot Write To A Running Spawned Process"