-1

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()
Squall Leohart
  • 657
  • 2
  • 8
  • 20
  • 1
    Yes, but what is your question? This currently reads like a "please implement X for me". What did you try, how did it fail, where did you get stuck? – Jean-Paul Calderone Jun 12 '12 at 17:27
  • My attempt was to generate a deferred object in lineReceived in the Echo protocol after the writeToChild line. Then, I did, addCallback(printData). printData is a function that broadcasts data to all instances of Echo. The problem was callback(result), I could not get the result from MyPP's outReceived. How does the deferred object know when outReceived returns a result? I would like to know if I am on the right track. My new approach that I am working on is to wrap the call writeToChild with a deferred object that would be returned. Then I will fire the callback in the outReceived. – Squall Leohart Jun 12 '12 at 18:16

1 Answers1

1

You need to implement childDataReceived(childFD, data) in your ProcessProtocol implementation. This will be called back when data arrives from the child process. See the API docs.

TheJuice
  • 4,434
  • 2
  • 26
  • 36