I am new to Twisted. I encountered this problem recently and would like some help. As I understand, the twisted reactor runs as a single-threaded loop. When I spawn another process to get the input from the loop (and later on would pass the result back to the loop), the process just quits right after, even when no input is passed to it (see my code below).
Also, is it possible to keep ONE spawned process running for a long time as opposed to spawning a new process every time an input is received?
Thank You for your time,
Q
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
implementation = """\
import os
print "in child", os.getpid()
"""
class Echo(Protocol):
def dataReceived(self, data):
reactor.spawnProcess(pp, executable, [executable, "-c", implementation], env=environ)
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", data
def errReceived(self, data):
print "error", data
def inConnectionLost(self):
print "inConnectionLost! stdin is closed! (we probably did it)"
def outConnectionLost(self):
print "outConnectionLost! The child closed their stdout!"
def errConnectionLost(self):
print "errConnectionLost! The child closed their stderr."
def processExited(self, reason):
print "processExited, status %d" % (reason.value.exitCode,)
def processEnded(self, reason):
print "processEnded, status %d" % (reason.value.exitCode,)
print "quitting"
reactor.listenTCP(8200, EchoFactory())
pp = MyPP()
print 'in parent', os.getpid()
reactor.spawnProcess(pp, executable, [executable, "-c", implementation], env=environ)
reactor.run()