[Twisted-Python] Advice on porting Python application to Twisted

Tom Sheffler tom.sheffler at gmail.com
Wed Aug 28 07:40:42 MDT 2013

Hi Matthew -

I have a couple of idioms I use for #2 and #3 in your message.  Here they


For timer events, I create a function that when called, continuously
schedules itself again in the reactor, does some work for the current
tick, and then exits.  I've used this down to 1-second intervals.  If
you're looking for sub-millisecond level timing, this may not be
appropriate for your application.

def timerFunction(reactor):

    reactor.callLater(1.0, timerFunction, reactor)

    # do the work for this time tick
    # etc etc


# Somewhere in main do this to kick it off
from twisted.internet import reactor


For subprocesses, I like to create a custom protocol for each type of
sub-command I am calling.  I also like to create an object to manage
the process, its arguments, its results and its temp files.  The idiom
below is suitable for calling a subprocess that accepts a small amount
of buffered data on stdin, produces some output on stdout, and logs its

Be careful examining the value of reason.value.exitCode in
processExited.  The twisted docs show printing the exitCode as a "%d",
but sometimes the value is None --- if the process was terminated by a
signal.  The mere printing of the value with "%d" will then trigger an

Here's my idiom:

class FooprocProtocol(protocol.ProcessProtocol):

    def __init__(self, foomgr):
        # the object managing my subprocess
        self.foomgr = foomgr

        # my stdout data
        self.data = ""

    def connectionMade(self):
        # Pump input data in using this, and then close stdin
        # self.transport.write("...")  # if there is any data to shove into

    def outReceived(self, data):
        # collect up our stdout
        log.msg("outReceived! with %d bytes!" % len(data))
        self.data = self.data + data

    def errReceived(self, data):
        # echo stderr messages to log with a marker
        log.msg(">%s" % data)

    def inConnectionLost(self):
        print "inConnectionLost! stdin is closed! (we probably did it)"

    def outConnectionLost(self):
        log.msg("outConnectionLost! The child closed their stdout!")

    def errConnectionLost(self):
        log.msg("errConnectionLost! The child closed their stderr.")

    def processExited(self, reason):
        log.msg("processExited:%s:" % reason)
        exitcode = reason.value.exitCode         # an integer or None

        # do some work upon processExit potentially make a decision on
exitcode ...

        log.msg("processExited:%s" % exitcode)

    def processEnded(self, reason):
        print "processEnded, status %s" % (reason.value.exitCode,)

        # process the data in the process manager
        exitcode = reason.value.exitCode         # might be non-numeric
        result = self.foomgr.processData(exitcode)

# The main job of the Process Manager is to build the command list and
# process the results.  It gives us a handy place to encapsulate this
# logic.

class FooprocManager(object):

    CMD = "/usr/local/foocmd"

    def __init__(self, arg1, arg2, arg3)

        # create a Deferred to fire when we succeed or fail
        self.d = Deferred()

        # build our command argument list as appropriate for our command
        self.cmdargs = self.build_cmd_args(arg1, arg2, arg3)

        # define places to store the transport, pid and other things
        self.ptransport = None
        self.pid = None

    def build_cmd_args(self, arg1, arg2, arg3):

        # in my projects, this method has become fairly involved as it
        # tmp files and builds potentially complicated argument lists.

        arglist = [self.CMD, arg1, arg2, arg3]
        return arglist

    def run(self):

        # instantiate a protocol connected to this manager
        pp = FooprocProtocol(self)

        # spawn the process, save the PID
        self.ptransport = reactor.spawnProcess(pp, self.CMD, self.cmdargs,
{ })
        self.pid = self.ptransport.pid

    def processData(exitcode):

        # in my projects, this method opens up result files, parses results,
        # moves things around, deletes tmp files, etc.

        # return the result that we ran this subprocess for
        return result

# Instantiate a new process manager and run it this way.

mgr = FooprocManager(args ...)
d = mgr.run(args ...)

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://twistedmatrix.com/pipermail/twisted-python/attachments/20130828/dbe47b07/attachment.html>

More information about the Twisted-Python mailing list