[Twisted-Python] Twisted Style

Edmund Dengler edmundd at eSentire.com
Sat Aug 9 16:57:06 EDT 2003

Hi all!

The Twisted stuff looks very good, and I think I am getting my head around
to the way it works. I am looking for how I should be approaching a basic
problem, and how to cast it in the Twisted model.

Basically, I have a client transferring database rows to a server. I would
like to transfer each row one-at-a-time, and I need information coming
back to know whether the transfer was successful and to record in the
client's database.

I have tried the following (please note that the code has been stripped
to show the essence):

Server (using PB)

Has a Perspective:

class MyPerspective(pb.Perspective):
    def attached(self, clientref, identity):

    def attached(self, ref, identity):

    def perspective_transferXXX(self, XXX):

...create an app and a service...
...create appropriate perspective and identity things...
...listen at a socket...

Client (1)

class TransferClient:
    def __init__(self):

    def go(self):
        ...connect to server, defer to connection()...

    def connection(self, perspective):
        # Connection to server has been made
        ...save away perspective...

    def transferCycle(self):
        # Start a whole cycle (each cycle may transfer multiple rows)

    def getXXXs(self):
        # Lets get the data to transfer
        ...setup database query, defer result set to transferXXXs()...

    def transferXXXs(self, XXXs):
        # We have our data from the query
        ...save away some state information...
        self.transferXXX(...first row...)

    def transferXXX(self, XXX):
        # Transfer a single row
        ...call remote perspective for XXX, defer return to doneXXX()...

    def doneXXX(self, result):
        # The row has been transferred and returned a result
        if successfully transferred:
            ...do appropriate work...
        if we still need to transfer more rows:
            self.transferXXX(...with the next row...)
            # Note: if we had other stuff, we would start them
            # transferring as well

    def cycleDone(self):
        # We have done a single cycle of transfers
        ...put a deferred for YYY seconds to start the cycle again...

Now, this seems like an awful lot of methods simply to support deferred
execution. Is there a way to use flows to acheive this? I have looked at
the HowTo pages, but it is not clear how to use the Flow components within
a PB model. Is there another approach?


More information about the Twisted-Python mailing list