Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #92807 > unrolled thread
| Started by | ravi <temp.sha@gmail.com> |
|---|---|
| First post | 2015-06-18 00:41 -0700 |
| Last post | 2015-06-19 00:14 +0200 |
| Articles | 4 — 3 participants |
Back to article view | Back to comp.lang.python
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
| From | ravi <temp.sha@gmail.com> |
|---|---|
| Date | 2015-06-18 00:41 -0700 |
| Subject | help in understanding the stackless code |
| Message-ID | <f0d40f43-36ee-4c41-9892-9b5ee524fea3@googlegroups.com> |
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.
[toc] | [next] | [standalone]
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2015-06-18 13:14 +0100 |
| Message-ID | <mailman.596.1434629697.13271.python-list@python.org> |
| In reply to | #92807 |
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?
[toc] | [prev] | [next] | [standalone]
| From | ravi <temp.sha@gmail.com> |
|---|---|
| Date | 2015-06-18 12:41 -0700 |
| Message-ID | <8e34cc5a-9577-4f63-b08a-fe70aa406621@googlegroups.com> |
| In reply to | #92824 |
yes It has instance of both Reporter and Switch.
moreover I could not get why instance "reporter" is passed to class Switch
as a parameter ?
> > reporter = Reporter()
> > switch = Switch(0,reporter)
> > switch(1)
thanks
On Thursday, June 18, 2015 at 5:45:08 PM UTC+5:30, MRAB wrote:
> 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?
[toc] | [prev] | [next] | [standalone]
| From | Laura Creighton <lac@openend.se> |
|---|---|
| Date | 2015-06-19 00:14 +0200 |
| Message-ID | <mailman.616.1434665683.13271.python-list@python.org> |
| In reply to | #92807 |
You need to send your message over here. http://www.stackless.com/mailman/listinfo/stackless I think I know the answer, from my work in duplicating stackless for greenlets in pypy. But that's the answer in theory. In practice, you need real stackless users. Laura
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web