Twisted Python: how to make a deferred object that waits for data from child process? -
in method linereceived in echo protocol, generate deferred object each time write child. wait result, , print result once data done being processed child process. need find way that. right now, mypp's function outreceived gets result child process.
thank
from sys import executable os import environ import os twisted.internet import reactor, defer twisted.internet.protocol import protocol twisted.internet.protocol import factory twisted.internet import protocol twisted.protocols.basic import linereceiver import sys import time import math implementation = """ 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()
you need implement childdatareceived(childfd, data)
in processprotocol
implementation. called when data arrives child process. see the api docs.
Comments
Post a Comment