Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #92807

help in understanding the stackless code

Newsgroups comp.lang.python
Date 2015-06-18 00:41 -0700
Message-ID <f0d40f43-36ee-4c41-9892-9b5ee524fea3@googlegroups.com> (permalink)
Subject help in understanding the stackless code
From ravi <temp.sha@gmail.com>

Show all headers | View raw


hi,
I am new to python and need to know why the calling of switch(1) invokes the function "listen" twice in the below program?



import stackless

class EventHandler:
    def __init__(self,*outputs):
        if outputs==None:
            self.outputs=[]
        else:
            self.outputs=list(outputs)

        self.channel = stackless.channel()
        stackless.tasklet(self.listen)()

    def listen(self):
        print "in listen()..."
        while 1:
            val = self.channel.receive()
            self.processMessage(val)
            for output in self.outputs:
                self.notify(output)
                
    def processMessage(self,val):
        pass

    def notify(self,output):
        pass

    def registerOutput(self,output):
        print "in registerOutput()..."
        self.outputs.append(output)
    
    def __call__(self,val):
        print "in __call__ ..."
        self.channel.send(val)

class Switch(EventHandler):
    def __init__(self,initialState=0,*outputs):
        EventHandler.__init__(self,*outputs)
        self.state = initialState

    def processMessage(self,val):
        print "in processMessage() of Switch..."
        self.state = val

    def notify(self,output):
        print "in notify() of switch..."
        output((self,self.state))

class Reporter(EventHandler):
    def __init__(self,msg="%(sender)s send message %(value)s"):
        EventHandler.__init__(self)
        self.msg = msg
        
    def processMessage(self,msg):
        print "in processMessage() of Reporter..."
        sender,value=msg
        print self.msg % {'sender':sender,'value':value}


if __name__ == "__main__":
    reporter = Reporter()
    switch = Switch(0,reporter) 
    switch(1)




output:
=========

in __call__ ...
in listen()...
in listen()...
in processMessage() of Switch...
in notify() of switch...
in __call__ ...
in processMessage() of Reporter...
<__main__.Switch instance at 0x8d822cc> send message 1





thanks.

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

help in understanding the stackless code ravi <temp.sha@gmail.com> - 2015-06-18 00:41 -0700
  Re: help in understanding the stackless code MRAB <python@mrabarnett.plus.com> - 2015-06-18 13:14 +0100
    Re: help in understanding the stackless code ravi <temp.sha@gmail.com> - 2015-06-18 12:41 -0700
  Re: help in understanding the stackless code Laura Creighton <lac@openend.se> - 2015-06-19 00:14 +0200

csiph-web