[Twisted-Python] More Questions about Custom Reactors

Andrew Francis andrewfr_ice at yahoo.com
Sun Jan 21 15:57:08 EST 2007


Hello Colleagues :

I am trying to write a reactor for Stackless Python. I
posted previously here and in the Stackless mailing
list but I am still stuck.  My problems are in the
following areas :

1. How does the custom reactor drive Twisted?
2. How does self.interleave() work?
3. How to use deferreds if Twisted is running in a
seperate thread. Is callFromThread() used?
4. Some custom reactor implementations  inherit and
some reactors simply install say
threadedselectorreactor? Which technique is
recommended?
 
For now, I am operating under the assumption that
Stackless Python and Twisted ought to be executed  in
seperate threads. This is because if Twisted blocks
then Stackless blocks. Since I am taking this
approach, I am assuming I will have to use
threadedselectorreactor.

I want my Stackless Python application to do two
things :

1. Process HTTP based SOAP server requests
2. From the Stackless thread, make client.getPage()
calls and use a deferred to process the result.

To promote decoupling, I want Twisted and Stackless to
communicate via queues. So my code looks as follows -


def processResponse(self, httpServer):
     reply = self.myQueue.get()
     httpServer.write(reply.toxml().encode("utf-8"))  
            
     httpServer.finish()
     return

example 2.

postRequest = client.getPage(self.address, method =
'POST', headers = self.headers, postdata =
self.body)postRequest.addCallback(self.__handleResponse__).addErrback(self.__handleError__)
    

def __handleResponse__(self, pageData):
         self.queue.put(pageData)

I have looked at the reactors included in the
twisted.internet. I also looked at Blockingdemo.py. I
find BlockingDemo.py confusing (i.e., where is poll()
called? It does not seem to do anything.)

Question about interleave():

"""
Taken from threadedselectorreactor  

In order for Twisted to do its work in the main thread
(the thread that interleave is called from), a waker
function is necessary.  The waker function will be
called from a "background" thread with one argument:
func. The waker function's purpose is to call func()
from the main thread. Many GUI toolkits ship with
appropriate waker functions.
"""

Using wxReactor as a basis, if the following is my
custom Stackless reactor's run method

def run(self):
      self.startRunning(...)
      self.interleave(some_waker_function)
      self.stacklessApplication.Loop()    

1. How does this drive Twisted (in wxReactor there
seems to be a second loop that sleeps every
millisecond)? .
2. What should the "some_waker_function" be doing? Is
the interleave() method always necessary?
3. What is the background thread? Twisted? A
workerThread in threadedselectorreactor?
4. To use client.getPage() and callbacks from
Stackless to a Twisted reactor running in a thread,
must I use callFromThread? Will the Stackless thread
block?

Once again, any advice would be appreciated. I will
happily summarise on the Stackless Wiki. Sorry for
the long message.

Thank you,
Andrew 


    


 
____________________________________________________________________________________
Do you Yahoo!?
Everyone is raving about the all-new Yahoo! Mail beta.
http://new.mail.yahoo.com




More information about the Twisted-Python mailing list