[Twisted-Python] design of twisted application
Hey, I am trying to design a twisted application, (I'm pretty new to twisted and networking in general) and I feel like I'm not approaching it quite the right way and would appreciate your feedback. First part, is that I am trying to couple twisted with an existing graphics-type application, that already has it's own main loop, etc. Meaning, I need to be running other python code while I have the reactor running. Is the solution threading? f = MyFactory() reactor.connectTCP("localhost",22223,f) t = threading.Thread(target=reactor.run) t.run() runOtherCodeConcurrently() Is there a better solution? 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? Thank you very much for your help! Kurt
On 2 February 2011 07:15, Kurt Spindler <kespindler@gmail.com> wrote:
Hey, I am trying to design a twisted application, (I'm pretty new to twisted and networking in general) and I feel like I'm not approaching it quite the right way and would appreciate your feedback. First part, is that I am trying to couple twisted with an existing graphics-type application, that already has it's own main loop, etc. Meaning, I need to be running other python code while I have the reactor running. Is the solution threading?
Hopefully there is already a twisted reactor for your graphics library, what are you using?
f = MyFactory()
reactor.connectTCP("localhost",22223,f)
t = threading.Thread(target=reactor.run)
t.run()
runOtherCodeConcurrently()
Is there a better solution? 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?
You should be able to do this on the Factory that created the protocol instance. Michael
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#HowdoImakeinputo... 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#Whydoesittakealo... (gotta love that anchor!... http://bit.ly/gPvW7d)
On Wed, Feb 2, 2011 at 2:15 AM, Kurt Spindler <kespindler@gmail.com> wrote:
I am trying to design a twisted application, (I'm pretty new to twisted and networking in general) and I feel like I'm not approaching it quite the right way and would appreciate your feedback. First part, is that I am trying to couple twisted with an existing graphics-type application, that already has it's own main loop, etc. Meaning, I need to be running other python code while I have the reactor running. Is the solution threading?
I would guess so. Otherwise, you may have to significantly rework the graphics lib to fit into twisted. I was involved in a project where I joined cherrypy and twisted into a single process. I ran each in a separate thread. Note that you will need to become very familiar with callFromThread()---any twisted call you want to make from the graphics side will need to use this in case the reactor is blocking (e.g. stuck in a select() call). Also, if the graphics lib either manipulates or relies on certain signal behavior, you may need to install your own signal handlers which do all the processing that both the graphics lib and twisted expect. This is especially important if you start any processes from twisted (ProcessProtocol). Cheers, Jason -- Jason Rennie Research Scientist, ITA Software 617-714-2645 http://www.itasoftware.com/
participants (4)
-
Jason Rennie -
Kurt Spindler -
Lucas Taylor -
Michael Thompson