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


Groups > comp.lang.python > #92824

Re: help in understanding the stackless code

Path csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python@mrabarnett.plus.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; '"__main__":': 0.07; '__name__': 0.07; 'msg': 0.07; 'subject:code': 0.07; 'val': 0.07; 'subject:help': 0.07; '"in': 0.09; 'stackless': 0.09; 'python': 0.11; 'def': 0.14; 'output': 0.15; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'invokes': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'program?': 0.16; 'ravi': 0.16; 'received:192.168.1.4': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'skip:{ 30': 0.16; 'subclasses,': 0.16; 'wrote:': 0.16; 'pass': 0.22; 'import': 0.24; 'header:In- Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'skip:_ 20': 0.26; 'switch': 0.27; 'skip:e 30': 0.27; 'function': 0.30; 'print': 0.31; 'skip:s 30': 0.31; 'skip:_ 10': 0.32; 'received:84': 0.32; 'class': 0.33; 'skip:_ 30': 0.33; 'to:addr:python-list': 0.35; 'instance': 0.35; 'skip:o 20': 0.35; 'hi,': 0.37; 'subject:: ': 0.37; 'skip:p 20': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'skip:e 20': 0.39; 'subject:the': 0.40; 'why': 0.40; '__call__': 0.84
X-CM-Score 0.00
X-CNFS-Analysis v=2.1 cv=OoyysHLt c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=QrohdLjRRo4A:10 a=IkcTkHD0fZMA:10 a=EBOSESyhAAAA:8 a=CKV5Z0YFK73q__RG9QQA:9 a=XVXjXf7Io3ue_Fbc:21 a=c0ybROdDUlCCMjyF:21 a=QEXdDO2ut3YA:10
X-AUTH mrabarnett@:2500
Date Thu, 18 Jun 2015 13:14:46 +0100
From MRAB <python@mrabarnett.plus.com>
User-Agent Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.7.0
MIME-Version 1.0
To python-list@python.org
Subject Re: help in understanding the stackless code
References <f0d40f43-36ee-4c41-9892-9b5ee524fea3@googlegroups.com>
In-Reply-To <f0d40f43-36ee-4c41-9892-9b5ee524fea3@googlegroups.com>
Content-Type text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.596.1434629697.13271.python-list@python.org> (permalink)
Lines 87
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1434629697 news.xs4all.nl 2956 [2001:888:2000:d::a6]:50881
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:92824

Show key headers only | View raw


On 2015-06-18 08:41, ravi wrote:
> 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
>
Is it because EventHandler has 2 subclasses, namely Switch and
Reporter, and you have an instance of each?

Back to comp.lang.python | Previous | NextPrevious in thread | Next 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