[Twisted-Python] Best place to put application code

John Aherne johnaherne at rocs.co.uk
Fri Jan 2 05:14:40 EST 2009

One thing that has been puzzling me is where is the best place to put
application code.

The case I am using is straightforward TCP server with client connections
making simple requests and waiting for responses retrieved from database
tables to be sent back.

Reading the docs and looking at various examples provided in books and
documentation has left me a bit confused.

Is there a best practice for where application code should go - either the
protcocol class or the factory class or somewhere else. Does it actually

Is there any downside to putting application code in the protocol or
factory. What pitfalls are there for either approach.

I see examples where application code appears in both classes, but the
examples are very small so may not be indicative of what should be done.

In the docs I see reference to most of the code will be written in the
protocol class, but that seems to be referring to actually writing protocols
not application code. It also says that when the protocol needs to call
application code to make it a method call -  not to mix protocol and
application code. This could just mean creating some methods in the protocol
class to handle the task.

However, if the application code needs to run for 10-12 seconds looking up
database tables and accumulating results and waiting on  deferreds, should
all this code reside in the protocol class or the factory.

If I keep it in the protocol, then I already have my client connection to
write back to. So that seems to be the place to keep the code.

If I put the code in the factory, then I need to pass the client connection
so it can write back to the client. Or is there another way of doing this I
have missed.
The factory seems to be the place where other classes can be passed in and
the protocol can call them via self.factory. That seems to imply that
application code should be put into the factory, but I can't see any way of
passing back information from deferred results to the call from protocol.
It's ok if it was just a simple method call that returns a result, but if
the code has to run a series of deferreds then it will be the called method
that will have the result and it will need a means of writing this back to
the client. I don't think I can signal the protocol to say I now have the
result.Of course I could easily be mistaken. So please correct.

Of course if I passs the client connection to the factory, then it can use
this to write back. But that means passing around the client connection.
Should I avoid doing that or is that not a problem.

I hope I have explained myself clearly. I'm just looking for some guidance
and pointers to what is best to do or what is best to avoid.


John Aherne
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-python/attachments/20090102/94e1efd2/attachment.htm 

More information about the Twisted-Python mailing list