[Twisted-Python] design of twisted application

Lucas Taylor ltaylor.volks at gmail.com
Wed Feb 2 13:55:36 MST 2011


On Feb 2, 2011, at 12:15 AM, Kurt Spindler wrote:

>  Second question is that, in that other bunch of code, I need to be  
> able to write things to the socket. How can I access the instance of  
> my protocol subclass that is used by the factory, so I can make  
> protocolInstance.transport.write type calls? Or am I thinking about  
> this is a completely wrong way?
>


This may be my favorite FAQ answer:
http://twistedmatrix.com/trac/wiki/FrequentlyAskedQuestions#HowdoImakeinputononeconnectionresultinoutputonanother

You're not specifically asking that question, but the salient point is:

"These are just regular Python objects; you can put them into lists,  
dictionaries, or whatever other data structure is appropriate to your  
application"

You can access connected protocol instances using something like the  
pattern in that FAQ. Just keep a reference to the factory instance  
somewhere in your other code and access whatever data structure you  
choose to store the protocol references. MultiEchoFactory has a list  
of connected protocols ('echoers'). You could just keep one connected  
instance. Or a dictionary keyed by ip address, session id you  
generate, etc.

One other note... how you end up integrating with whatever GUI you are  
using may impact your use of Twisted APIs (i.e. transport.write).
See
http://twistedmatrix.com/trac/wiki/FrequentlyAskedQuestions#WhydoesittakealongtimefordataIsendwithtransport.writetoarriveattheothersideoftheconnection
(gotta love that anchor!... http://bit.ly/gPvW7d)

-------------- next part --------------
An HTML attachment was scrubbed...
URL: </pipermail/twisted-python/attachments/20110202/560cafeb/attachment.html>


More information about the Twisted-Python mailing list