In the method lineReceived in Echo protocol, I would like to generate a deferred object each time I write to child. It would wait for a result, and then print the result once the data is done being processed by the child process. I need to find a way to do that. Right now, MyPP's function outReceived gets the result from the child process.
Thank You
from sys import executable
from os import environ
import os
from twisted.internet import reactor, defer
from twisted.internet.protocol import Protocol
from twisted.internet.protocol import Factory
from twisted.internet import protocol
from twisted.protocols.basic import LineReceiver
import sys
import time
import math
implementation = """
from filter import censor
censor()
"""
class Echo(LineReceiver):
def lineReceived(self, data):
if self.factory.pp == None:
pp = MyPP()
self.factory.pp = reactor.spawnProcess(pp, executable, [executable, "-c", implementation],
env=environ, childFDs = {0:"w", 1:"r", 2:"r"})
self.factory.pp.writeToChild(0, data+'\r\n')
class EchoFactory(Factory):
protocol = Echo
pp = None
class MyPP(protocol.ProcessProtocol):
def connectionMade(self):
print "connectionMade!"
def outReceived(self, data):
pass
reactor.listenTCP(11111, EchoFactory())
print 'in parent', os.getpid()
reactor.run()