Re: python program without stackless.run()

Kristján Valur Jónsson <[email protected]> Mon, 22 Jun 2015 15:45:26 +0000
Newsgroups gmane.comp.python.stackless
Message-ID <CAP8kY6bcbPV0GSE6sKPOCrkGf_Li1-Y-tqOvc4kEsNNCti4-JA@mail.gmail.com>
--===============2801835259511938056==
Content-Type: multipart/alternative; boundary=e89a8f2357399c0afc05191d297d

--e89a8f2357399c0afc05191d297d
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

The stackless run queue is implicit and whenever a tasklet blocks (i.e.
when doing channel operations and sender/receiver isn't available), a
"runnable" tasklet from the run queue is fetched and switched to.
stackless.run() is a tool to suspend the current tasklet while there are
runnable tasklets in the queue.
You can achieve a similar effect by doing:
while stackless.runcount > 1:
    stackless.schedule()


On 22 June 2015 at 15:25, Anselm Kruis <[email protected]> wrote=
:

> Hi,
>
> I'm not so firm with the channel code, but I think it is fairly simple to
> explain. Have a look at the documentation of the tasklet life cycle:
> https://stackless.readthedocs.org/en/2.7-slp/library/stackless/tasklets.h=
tml#tasklet-life-cycle
>
> As you can see, tasklet can become "current" without a running scheduler.
> At the end of this section there is a simplified documentation of the
> internal working of the stackless.run() function. It does not enter a
> tasklet into the scheduler queue and it is not the only way to switch
> tasklets.
>
> Your example uses 2 tasklets:
> 1. the main tasklet
> 2. the tasklet "self.fun"
>
> The line "stackless.tasklet(self.fun)()" creates and schedules tasklet
> fun().
>
> Then the main tasklet sends a message to through the channel self.ch.
> This causes stackless to change the state of the main tasklet to "blocked
> on channel" (See
> https://stackless.readthedocs.org/en/2.7-slp/library/stackless/channels.h=
tml#channel.send
> "If no other tasklet is already receiving on the channel, the sender will
> be blocked."). At the same moment the next tasklet on the schedule queue,
> that is tasklet "self.fun" becomes "current". It receives the message fro=
m
> the channel self.ch. This changes the state of the main tasklet from
> "blocked on channel" to "scheduled". Then tasklet self.fun runs to the en=
d
> of method self.fun and terminates. This again triggers the scheduler and
> now the main tasklet becomes current again.
>
> To observe the details, you may want to study the tracing example at
>
> https://bitbucket.org/stackless-dev/stackless/src/9f2a496e6ba2cf9680e3574=
51a8d75821d0ebb72/Stackless/demo/tracing.py?at=3D2.7-slp
>
> Regards
>   Anselm
>
>
>
> Am 22.06.2015 um 13:55 schrieb temp sha:
>
>> hi,
>>
>> any help in understanding the program will be highly appreciated.
>> as per the stackless documentation stackless.run() is must
>> to start a scheduler. then how come the below program runs
>> the tasklet "fun" without stackless.run() call?
>>
>>
>> thanks in advance.
>>
>>
>> On Fri, Jun 19, 2015 at 2:36 AM, temp sha <[email protected]> wrote:
>>
>>> Hi,
>>>
>>> I could not understand how the below program executes function "fun"
>>> without calling stackless.run() in the program?  Here "fun" runs as a
>>> tasklet and as per my knowledge for that stackless.run() is must.
>>>
>>>
>>>
>>> -----------------------------------------------------------------
>>> import stackless
>>>
>>> class A:
>>>      def __init__(self,name):
>>>          self.name =3D name
>>>          self.ch =3D stackless.channel()
>>>          stackless.tasklet(self.fun)()
>>>
>>>      def __call__(self,val):
>>>          self.ch.send(val)
>>>
>>>      def fun(self):
>>>         while 1:
>>>           v =3D self.ch.receive()
>>>           print "hi" , v
>>>
>>>
>>> if __name__ =3D=3D "__main__":
>>>      obj =3D A("sh")
>>>      obj(6)
>>> -----------------------------------------------------------------
>>>
>>> output:
>>> ----------
>>> hi 6
>>>
>>>
>>>
>>>
>>>
>>> thanks,
>>> ravi
>>>
>>
>> _______________________________________________
>> Stackless mailing list
>> [email protected]
>> http://www.stackless.com/mailman/listinfo/stackless
>>
>>
> --
>  Dipl. Phys. Anselm Kruis                       science + computing ag
>  Senior Solution Architect                      Ingolst=C3=A4dter Str. 22
>  email [email protected]             80807 M=C3=BCnchen, Germa=
ny
>  phone +49 89 356386 874  fax 737               www.science-computing.de
>
> --
> Vorstandsvorsitzender/Chairman of the board of management:
> Gerd-Lothar Leonhart
> Vorstand/Board of Management:
> Dr. Bernd Finkbeiner, Dr. Arno Steitz
> Vorsitzender des Aufsichtsrats/
> Chairman of the Supervisory Board:
> Philippe Miltin
> Sitz/Registered Office: Tuebingen
> Registergericht/Registration Court: Stuttgart
> Registernummer/Commercial Register No.: HRB 382196
>
>
>
> _______________________________________________
> Stackless mailing list
> [email protected]
> http://www.stackless.com/mailman/listinfo/stackless
>

--e89a8f2357399c0afc05191d297d
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">The stackless run queue is implicit and whenever a tasklet=
 blocks (i.e. when doing channel operations and sender/receiver isn&#39;t a=
vailable), a &quot;runnable&quot; tasklet from the run queue is fetched and=
 switched to.<div>stackless.run() is a tool to suspend the current tasklet =
while there are runnable tasklets in the queue.</div><div>You can achieve a=
 similar effect by doing:</div><div>while stackless.runcount &gt; 1:<br>=C2=
=A0 =C2=A0 stackless.schedule()</div><div><br></div></div><div class=3D"gma=
il_extra"><br><div class=3D"gmail_quote">On 22 June 2015 at 15:25, Anselm K=
ruis <span dir=3D"ltr">&lt;<a href=3D"mailto:[email protected]" =
target=3D"_blank">[email protected]</a>&gt;</span> wrote:<br><bl=
ockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #=
ccc solid;padding-left:1ex">Hi,<br>
<br>
I&#39;m not so firm with the channel code, but I think it is fairly simple =
to explain. Have a look at the documentation of the tasklet life cycle: <a =
href=3D"https://stackless.readthedocs.org/en/2.7-slp/library/stackless/task=
lets.html#tasklet-life-cycle" rel=3D"noreferrer" target=3D"_blank">https://=
stackless.readthedocs.org/en/2.7-slp/library/stackless/tasklets.html#taskle=
t-life-cycle</a><br>
<br>
As you can see, tasklet can become &quot;current&quot; without a running sc=
heduler. At the end of this section there is a simplified documentation of =
the internal working of the stackless.run() function. It does not enter a t=
asklet into the scheduler queue and it is not the only way to switch taskle=
ts.<br>
<br>
Your example uses 2 tasklets:<br>
1. the main tasklet<br>
2. the tasklet &quot;self.fun&quot;<br>
<br>
The line &quot;stackless.tasklet(self.fun)()&quot; creates and schedules ta=
sklet fun().<br>
<br>
Then the main tasklet sends a message to through the channel <a href=3D"htt=
p://self.ch" rel=3D"noreferrer" target=3D"_blank">self.ch</a>. This causes =
stackless to change the state of the main tasklet to &quot;blocked on chann=
el&quot; (See <a href=3D"https://stackless.readthedocs.org/en/2.7-slp/libra=
ry/stackless/channels.html#channel.send" rel=3D"noreferrer" target=3D"_blan=
k">https://stackless.readthedocs.org/en/2.7-slp/library/stackless/channels.=
html#channel.send</a> &quot;If no other tasklet is already receiving on the=
 channel, the sender will be blocked.&quot;). At the same moment the next t=
asklet on the schedule queue, that is tasklet &quot;self.fun&quot; becomes =
&quot;current&quot;. It receives the message from the channel <a href=3D"ht=
tp://self.ch" rel=3D"noreferrer" target=3D"_blank">self.ch</a>. This change=
s the state of the main tasklet from &quot;blocked on channel&quot; to &quo=
t;scheduled&quot;. Then tasklet self.fun runs to the end of method self.fun=
 and terminates. This again triggers the scheduler and now the main tasklet=
 becomes current again.<br>
<br>
To observe the details, you may want to study the tracing example at<br>
<a href=3D"https://bitbucket.org/stackless-dev/stackless/src/9f2a496e6ba2cf=
9680e357451a8d75821d0ebb72/Stackless/demo/tracing.py?at=3D2.7-slp" rel=3D"n=
oreferrer" target=3D"_blank">https://bitbucket.org/stackless-dev/stackless/=
src/9f2a496e6ba2cf9680e357451a8d75821d0ebb72/Stackless/demo/tracing.py?at=
=3D2.7-slp</a><br>
<br>
Regards<br>
=C2=A0 Anselm<div class=3D"HOEnZb"><div class=3D"h5"><br>
<br>
<br>
Am 22.06.2015 um 13:55 schrieb temp sha:<br>
<blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p=
x #ccc solid;padding-left:1ex">
hi,<br>
<br>
any help in understanding the program will be highly appreciated.<br>
as per the stackless documentation stackless.run() is must<br>
to start a scheduler. then how come the below program runs<br>
the tasklet &quot;fun&quot; without stackless.run() call?<br>
<br>
<br>
thanks in advance.<br>
<br>
<br>
On Fri, Jun 19, 2015 at 2:36 AM, temp sha &lt;<a href=3D"mailto:temp.sha@gm=
ail.com" target=3D"_blank">[email protected]</a>&gt; wrote:<br>
<blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p=
x #ccc solid;padding-left:1ex">
Hi,<br>
<br>
I could not understand how the below program executes function &quot;fun&qu=
ot;<br>
without calling stackless.run() in the program?=C2=A0 Here &quot;fun&quot; =
runs as a<br>
tasklet and as per my knowledge for that stackless.run() is must.<br>
<br>
<br>
<br>
-----------------------------------------------------------------<br>
import stackless<br>
<br>
class A:<br>
=C2=A0 =C2=A0 =C2=A0def __init__(self,name):<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0<a href=3D"http://self.name" rel=3D"noref=
errer" target=3D"_blank">self.name</a> =3D name<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0<a href=3D"http://self.ch" rel=3D"norefer=
rer" target=3D"_blank">self.ch</a> =3D stackless.channel()<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0stackless.tasklet(self.fun)()<br>
<br>
=C2=A0 =C2=A0 =C2=A0def __call__(self,val):<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0self.ch.send(val)<br>
<br>
=C2=A0 =C2=A0 =C2=A0def fun(self):<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 while 1:<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 v =3D self.ch.receive()<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 print &quot;hi&quot; , v<br>
<br>
<br>
if __name__ =3D=3D &quot;__main__&quot;:<br>
=C2=A0 =C2=A0 =C2=A0obj =3D A(&quot;sh&quot;)<br>
=C2=A0 =C2=A0 =C2=A0obj(6)<br>
-----------------------------------------------------------------<br>
<br>
output:<br>
----------<br>
hi 6<br>
<br>
<br>
<br>
<br>
<br>
thanks,<br>
ravi<br>
</blockquote>
<br>
_______________________________________________<br>
Stackless mailing list<br>
<a href=3D"mailto:[email protected]" target=3D"_blank">Stackless@stac=
kless.com</a><br>
<a href=3D"http://www.stackless.com/mailman/listinfo/stackless" rel=3D"nore=
ferrer" target=3D"_blank">http://www.stackless.com/mailman/listinfo/stackle=
ss</a><br>
<br>
</blockquote>
<br></div></div><span class=3D"HOEnZb"><font color=3D"#888888">
-- <br>
=C2=A0Dipl. Phys. Anselm Kruis=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0science + computing ag<br>
=C2=A0Senior Solution Architect=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 Ingolst=C3=A4dter Str. 22<br>
=C2=A0email <a href=3D"mailto:[email protected]" target=3D"_blan=
k">[email protected]</a>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A080807 M=C3=BCnchen, Germany<br>
=C2=A0phone <a href=3D"tel:%2B49%2089%20356386%20874" value=3D"+49893563868=
74" target=3D"_blank">+49 89 356386 874</a>=C2=A0 fax 737=C2=A0 =C2=A0 =C2=
=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0<a href=3D"http://www.science-computi=
ng.de" rel=3D"noreferrer" target=3D"_blank">www.science-computing.de</a><br=
>
<br>
-- <br>
Vorstandsvorsitzender/Chairman of the board of management:<br>
Gerd-Lothar Leonhart<br>
Vorstand/Board of Management:<br>
Dr. Bernd Finkbeiner, Dr. Arno Steitz<br>
Vorsitzender des Aufsichtsrats/<br>
Chairman of the Supervisory Board:<br>
Philippe Miltin<br>
Sitz/Registered Office: Tuebingen<br>
Registergericht/Registration Court: Stuttgart<br>
Registernummer/Commercial Register No.: HRB 382196</font></span><div class=
=3D"HOEnZb"><div class=3D"h5"><br>
<br>
<br>
_______________________________________________<br>
Stackless mailing list<br>
<a href=3D"mailto:[email protected]" target=3D"_blank">Stackless@stac=
kless.com</a><br>
<a href=3D"http://www.stackless.com/mailman/listinfo/stackless" rel=3D"nore=
ferrer" target=3D"_blank">http://www.stackless.com/mailman/listinfo/stackle=
ss</a><br>
</div></div></blockquote></div><br></div>

--e89a8f2357399c0afc05191d297d--


--===============2801835259511938056==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
Stackless mailing list
[email protected]
http://www.stackless.com/mailman/listinfo/stackless
--===============2801835259511938056==--