[Twisted-Python] Newbie question: can't get getProcessOutput to run from inside a listenTCP service call-back

Read Roberts rroberts at adobe.com
Sat Oct 17 00:15:03 EDT 2009

I have a Python server using twisted which responds to pyAMF calls from
Flex/Air applications (following Bruce Eckels 5/1/2008 article "Concurrency
with Python, Twisted, and Flex"). The service is basically as follows below.
When the client calls a service API, the API needs to make a OS shell call.
I see that if I call reactor.stop() after the  getProcessOutput call (such
as within the doLog and doError functions), the call happens and I see the
output. How can can I retrieve the output without calling reactor.stop()? In
the example below, the AIR app calls the callback helloWorldTest(), and the
deferred.addCallback function is never called.

import os
from twisted.spread import pb
from  twisted.internet.utils import getProcessOutput
from twisted.internet import reactor

class FlexInterface(pb.Root):
    def __init__(self):
        self.result e

    def doLog(self, result):
        self.result = "%s" % (result)
        return result
    def doError(self, failure):
        self.result = "%s" % ( failure)
        return failure

    def helloWorldTest(self):
        deferred = getProcessOutput("echo", ["Hello World"], os.environ)
        while (self.result == None)
            print "doLog still hasn't been called"
        return self.result

    def terminate(self, discardPostAag):
        reactor.callLater(1, reactor.stop)
        return "Terminating CubeGlyphServices"

def run():
    # Place the namespace mapping into a TwistedGateway:
    fi = FlexInterface()
    gateway = TwistedGateway({ "flexAppServer": fi })

    # Publish the PyAMF gateway at the root URL:
    root = resource.Resource()
    root.putChild("", gateway)

    # Tell the twisted reactor to listen:
    reactor.listenTCP(8050, server.Site(root))
    print "Local python server listening on localhost port 8050"

if __name__=='__main__':

I do know that If I used Python 2.6 and later, a twisted bug is fixed such
that I could use subprocess.Popen, and that the next release of AIR will
allow direct execv calls. However, for performance issues  I still want to
call one Python process running a server, and I have to work within a Python
2.5 environment.

Thanks for any advice,
 Read Roberts

More information about the Twisted-Python mailing list