How to get the status of spawn process in twisted python? -
i want trigger many long running processes continiously. and, based on status returned of each process executed, need perform other tasks. in below example, i'm able spawn processes, i'm not able capture/get details of spawn processes execution status returned mail loop(i.e in cmdprotocol class).
i'm new twisted python concepts - can me here?
import sys twisted.internet.protocol import serverfactory, processprotocol twisted.protocols.basic import linereceiver twisted.internet import reactor 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 closed! (we did it)" print "parent id = %s" % self.parent_id print "child process id closes stdin= %s" % self.transport.pid def outconnectionlost(self): print "outconnectionlost! child closed stdout!" print "parent id = %s" % self.parent_id print "child process id closes stdout = %s" % self.transport.pid def errconnectionlost(self): print "errconnectionlost! child closed 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 %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 command print 'cmd received %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 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()
this basic python data structures question. need refer instance of cmdprotocol
instance of mypp
. since cmdprotocol
constructs mypp
in first place, easy. change construction of mypp
this:
def processcmd(self): pp = mypp(self)
and mypp.__init__
this:
def __init__(self, cmd_protocol): self.parent_id = os.getpid() self.cmd_protocol = cmd_protocol
then, in method on mypp
, can access relevant cmdprotocol
instance self.cmd_protocol
.
Comments
Post a Comment