#!/usr/bin/env python
"""
TestTasklet.py
Andrew Francis
August 15th, 2007

<song>Rock and Roll - Led Zeppelin
"""

from twisted.python            import log
from StacklessTwistedReactor   import StacklessTwistedReactor
from StacklessTwistedReactor   import TwistedException
import stackless
import sys

"""
The TestTasklet makes a series of calls to the processor
"""
class TestTasklet(object):
    def __init__(self, processor):
        self.processor = processor
        return
    
    def execute(self):
        try:
            
            """
            the processor's underlying machinery hides all the
            Twisted deferred information
            
            actual Twisted call client.getPage() that returns a deferred
            """
            result = self.processor.getPage('http://www.google.com')
            
            #print the message
            print result
            
        except TwistedException:
            log.msg("A Twisted Exception was thrown")
            log.msg(sys.exc_info())
            
        except:
            log.msg(sys.exc_info()) 
        return


if __name__ == "__main__":
    processor = StacklessTwistedReactor()
    processor.run()
    stackless.tasklet(TestTasklet(processor).execute)()
    
    while (stackless.getruncount() > 1):
        stackless.schedule()