1

我想连续触发许多长时间运行的进程。并且,根据每个执行的进程返回的状态,我需要执行其他任务。在下面的示例中,我能够生成进程,但我无法捕获/获取返回到邮件循环的生成进程执行状态的详细信息(即在 CmdProtocol 类中)。

我是这个扭曲的 python 概念的新手——有人可以帮我吗?

import sys
from twisted.internet.protocol import ServerFactory, ProcessProtocol
from twisted.protocols.basic import LineReceiver
from twisted.internet import reactor
from twisted.internet import protocol
import os
import signal


class MyPP(protocol.ProcessProtocol):
    def __init__(self):
        self.parent_id = os.getpid()

    def connectionMade(self):
        print "connectionMade!"
        print "Parent id = %s" % self.parent_id
        print "Child process id = %s" % self.transport.pid

    def outReceived(self, data):
        print "out", data

    def errReceived(self, data):
        print "error", data

    def inConnectionLost(self):
        print "inConnectionLost! stdin is closed! (we probably did it)"
        print "Parent id = %s" % self.parent_id
        print "Child process id closes STDIN= %s" % self.transport.pid    

    def outConnectionLost(self):
        print "outConnectionLost! The child closed their stdout!"
        print "Parent id = %s" % self.parent_id
        print "Child process id closes STDOUT = %s" % self.transport.pid                

    def errConnectionLost(self):
        print "errConnectionLost! The child closed their stderr."
        print "Parent id = %s" % self.parent_id
        print "Child process id closes ERRCONN = %s" % self.transport.pid    

    def processExited(self, reason):
        print "processExited %s, status %d" % (self.transport.pid, reason.value.exitCode,)

    def processEnded(self, reason):
        print "%s processEnded, status %d" % (self.transport.pid, reason.value.exitCode,)
        print "quitting"

class CmdProtocol(LineReceiver):

    delimiter = '\n'
    def connectionMade(self):
        self.client_ip = self.transport.getPeer()
        print "Client connection from %s" % self.client_ip

    def processcmd(self):
        pp = MyPP()
        cmd = ['c:\Python27\python.exe', '-u',  'print_hi.py']
        print "Calling processcmd - <%s>" % cmd
        reactor.spawnProcess(pp, cmd[0], cmd[1:])

    def connectionLost(self, reason):
        print "Lost client connection.  Reason: %s" % reason

    def lineReceived(self, line):
        if not line: return
        # Parse the command
        print 'Cmd received from %s : %s' % (self.client_ip, line)
        commandParts = line.split()

        if len(commandParts) > 0:
            command = commandParts[0].lower()
            args = commandParts[1:]
            try:
                print "Command received : <%s>" % command
                method = getattr(self, command)
            except AttributeError, e:
                self.sendLine('Error: no such command.')
            else:
                try:
                    res = method()
                    print "Returned status:%s" % res
                    self.sendLine('Command executed successfully.')                
                except Exception, e:
                    self.sendLine('Error: ' + str(e))     

    def do_kill(self, pid):
        """kill: Kill a process (PID)"""
        print 'Killing pid:%s' % pid
        res = os.kill(int(pid), signal.SIGTERM)
        print "Kill Status %s" % res

class MyFactory(ServerFactory):
    protocol = CmdProtocol
    def __init__(self):
        print "Factory called"

reactor.listenTCP(8000, MyFactory())
reactor.run()
4

1 回答 1

1

这实际上是一个非常基本的 Python 数据结构问题。您只需CmdProtocol要从MyPP. 既然CmdProtocolMyPP首先构建的东西,这很容易。只需将结构更改为MyPP如下所示:

def processcmd(self):
    pp = MyPP(self)

然后MyPP.__init__看起来像这样:

def __init__(self, cmd_protocol):
    self.parent_id = os.getpid()
    self.cmd_protocol = cmd_protocol

然后,在 上的任何方法中MyPP,您都可以使用 访问相关CmdProtocol实例self.cmd_protocol

于 2013-08-07T18:47:25.230 回答