1

我的问题是,在产生一个进程之后,子进程正在循环以从其标准输入中获取数据。我想使用 Echo.Process.pipes[0].write(data) 或 Echo.Process.writeToChild(0,data) 向它写入新数据,但两者都不起作用。有人会解释发生了什么吗?或者我该如何解决这个问题?

这是我得到的错误:

--- <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

谢谢你,

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()
4

2 回答 2

2

在每个传入连接上创建一个新进程并将所有输入数据重定向到进程的标准输入:

#!/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()
于 2012-06-08T01:22:24.793 回答
0

不要传递childFDsspawnProcess也不要使用pipes生成的进程传输对象的属性。这些事情都不是你想的那样。如果您放弃使用childFDs并切换回writeToChild,您将获得所需的行为。

于 2012-06-08T13:09:22.780 回答