[Stackless] Blocking Problem with Stackless Python and Twisted

Andrew Francis andrewfr_ice at yahoo.com
Thu Jun 15 20:47:45 CEST 2006


Hello Colleagues:

I wrote a test programme that acts both as a web
server and a web client. Some tasklets make HTTP
requests. Other tasklets service incoming HTTP
requests.

My problem is that is that my code is sensitive to the
order of processing client and server requests. If a
tasklet acting as a cgi script (server) is called
first, everything else will block until I send the
test programme a HTTP request. Given the way the code
is written, I was not expecting this. 

My sneaking suspicion is that Twisted itself is
blocked. Off hand I don't know if Requests are
initiated as deferred objects. I am going through
Twisted code. Meanwhile, I am hoping someone out there
has insights. Thanks in advance.

I use channels to enable the Twisted code to
communicate with tasklets. For instance:

class TwistedConnection(object):
    def __init__(self, URL, body, headers, channel):
        self.channel = channel
        ...
        
    def __handleResponse__(self, pageData):
        self.channel.send(pageData)


    def connect(self):
        postRequest = client.getPage(url, method =    
\'POST', headers = self.headers, postdata= self.body)
            
postRequest.addCallback(self.__handleResponse__).addErrback(self.__handleError__)


and in a client, I have the following

def execute(self):
    channel = stackless.channel()
    connection = TwistedConnection(....,channel)
    connection.connect()
    """
    client should yield until something happens
    """
    result = channel.receive()


my server code also uses channels ...


Handler(http.Request):
    
     def process(self):
         
         #create a private channel
         myChannel = stackless.channel()

         """
         send information to a tasklet acting like a
         cgi script - make sure to send back a reply
         channel
         """

         Handler.channel.send((myChannel, info)

         #now read the result
         cgiResult = myChannel.receive()
         
         #write result back to client
         self.write(cgiResult)



class Client(object):
      def __init__(self, webServerChannel):
          self.webServerChannel = webServerChannel

      def execute(self):
          while (1):
             result = self.webServerChannel.receive()
         
      
class TwistedWebServer(object):
     def execute(self):
         reactor.listenTCP(port, TwistedHttpFactory())
         reactor.run()


The main body of the code looks as follows

server = TwistedWebServer(8000, outputChannel)
client1 = Client("one")
cleint2 = Client("two")
cgi = CGI("cgi", outputChannel)
        
print "[running server]"
stackless.tasklet(server.execute)()
        
print "[running cgi]"
stackless.tasklet(cgi.execute)()
print "[running client one]"
stackless.tasklet(client1.execute)()
print "[running client two]"
stackless.tasklet(client2.execute)()
                
while (stackless.getruncount() > 1):
      stackless.schedule()


Cheers,
Andrew

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

_______________________________________________
Stackless mailing list
Stackless at stackless.com
http://www.stackless.com/mailman/listinfo/stackless



More information about the Stackless mailing list