Twisted Python: Cannot write to a running spawned process

微笑、不失礼 提交于 2019-12-02 07:37:00

To create a new process on each incoming connection and to redirect all input data to the process' stdin:

#!/usr/bin/python
from twisted.internet import reactor

from twisted.internet import protocol

class Echo(protocol.Protocol):
    def connectionMade(self):
        self.pp = MyPP()
        reactor.spawnProcess(self.pp, 'cat', ['cat'])
    def dataReceived(self, data):
        self.pp.transport.write(data)
    def connectionLost(self, reason):
        self.pp.transport.loseConnection()

class MyPP(protocol.ProcessProtocol):
    def connectionMade(self):
        print "connectionMade!"
    def outReceived(self, data):
        print "out", data,
    def errReceived(self, data):
        print "error", data,
    def processExited(self, reason):
        print "processExited"
    def processEnded(self, reason):
        print "processEnded"
        print "quitting"

factory = protocol.Factory()
factory.protocol = Echo
reactor.listenTCP(8200, factory)
reactor.run()

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!