<html dir="ltr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Windows-1252">
<style id="owaParaStyle">P {
        MARGIN-BOTTOM: 0px; MARGIN-TOP: 0px
}
</style>
</head>
<body fPStyle="1" ocsi="0">
<div style="direction: ltr;font-family: Tahoma;color: #000000;font-size: 10pt;">
<p>    > I think I'm confused :-)</p>
<p>No wonder.  Let's assume you have a dead tasklet, S.  </p>
<p>S.run() and S.insert()</p>
<p>will both throw a RuntimeError, complaining that a dead tasklet can't be run.</p>
<p>but s.raise_exception(IndexError)</p>
<p>will happily work.  the tasklet _will_ be scheduled to run and will raise this exception and die again.  In effect, you are
<em>resurrecting the tasklet</em>, rebuilding it to a NULL frame, and running it again.  But a tasklet with a "null" frame is not really legal.  And resurrecting taskletss is illegal.  The reason this works currently is because of a quirk in the scheduling
 code.</p>
<p>And because s.throw() now supports non-immediate exception delivery, this behaviour can't be emulated safely.  PyTasklet_Insert() won't accept a dead tasklet.</p>
<p>So, I think it is cleaner to just disallow this, like s.run() and s.insert() already do.</p>
<p> </p>
<div>> def tasklet_bootstrap(f, *args, **kwargs):</div>
<div>>   try:</div>
<div>>       ret = f(*args, **kwargs)</div>
<div>>   except:</div>
<div>>       # handle</div>
<div>> stackless.set_callable_wrapper(tasklet_bootstrap)</div>
<div>This is currently indirectly possible, by setting stackless.tasklettype (IIRC) wehre you can put a tasklet subclass that overrides the __new__ etc.</div>
<div>But the problem with both approaches is that an exception delivered before the tasklet has had the chance to run for the first time won't be handled by this.  Hence my suggestion to set a global handler.  Having a module level handler is fine, IMO.  Adding
 such a beast is also simple to do.  Perhaps I'll make the experiment.</div>
<div> </div>
<div>K</div>
<p> </p>
<div style="FONT-SIZE: 16px; FONT-FAMILY: Times New Roman; COLOR: #000000">
<hr tabindex="-1">
<div id="divRpF832594" style="DIRECTION: ltr"><font color="#000000" size="2" face="Tahoma"><b>Frá:</b> Richard Tew [richard.m.tew@gmail.com]<br>
<b>Sent:</b> 5. apríl 2013 20:56<br>
<b>To:</b> Kristján Valur Jónsson<br>
<b>Cc:</b> The Stackless Python Mailing List<br>
<b>Efni:</b> Re: [Stackless] added tasklet.throw<br>
</font><br>
</div>
<div></div>
<div>
<div dir="ltr">On Sat, Apr 6, 2013 at 5:13 AM, Kristján Valur Jónsson <span dir="ltr">
<<a href="mailto:kristjan@ccpgames.com" target="_blank">kristjan@ccpgames.com</a>></span> wrote:<br>
<div class="gmail_extra">
<div class="gmail_quote">
<blockquote class="gmail_quote" style="PADDING-LEFT: 1ex; MARGIN: 0px 0px 0px 0.8ex; BORDER-LEFT: rgb(204,204,204) 1px solid">
<div lang="EN-GB" bgcolor="white">
<div>
<p><span style="FONT-SIZE: 11pt; FONT-FAMILY: Calibri,sans-serif; COLOR: rgb(31,73,125)">Ok, I have committed a change to the 2.7 branch (didn’t want to do the full merging yet, until we stabilize):<u></u><u></u></span></p>
<p><span style="FONT-SIZE: 11pt; FONT-FAMILY: Calibri,sans-serif; COLOR: rgb(31,73,125)">Here’s the comment:<u></u><u></u></span></p>
<p><span style="FONT-SIZE: 11pt; FONT-FAMILY: Calibri,sans-serif; COLOR: rgb(31,73,125)"><u></u><u></u></span> </p>
<p style="MARGIN-LEFT: 36pt"><span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; COLOR: windowtext">modifying edge case semantics of tasklet.throw()<u></u><u></u></span></p>
<p style="MARGIN-LEFT: 36pt"><span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; COLOR: windowtext">1) sending to a new tasklet works like a running tasklet.  It is scheduled,<u></u><u></u></span></p>
<p style="MARGIN-LEFT: 36pt"><span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; COLOR: windowtext">gets an unhandled exception, and things proceed as per usual in that case.<u></u><u></u></span></p>
<p style="MARGIN-LEFT: 36pt"><span style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; COLOR: windowtext">2) Sending to a dead tasklet causes a RuntimeError, unless one is sending<u></u><u></u></span></p>
<p style="MARGIN-LEFT: 36pt"><span style="COLOR: windowtext">a TaskletExit, in which case it is just ignored.</span><span style="FONT-SIZE: 11pt; FONT-FAMILY: Calibri,sans-serif; COLOR: rgb(31,73,125)"><u></u><u></u></span></p>
<p><span style="FONT-SIZE: 11pt; FONT-FAMILY: Calibri,sans-serif; COLOR: rgb(31,73,125)"><u></u><u></u></span> </p>
<p><span style="FONT-SIZE: 11pt; FONT-FAMILY: Calibri,sans-serif; COLOR: rgb(31,73,125)">This is now pretty much the same as tasklet.raise_exception().<u></u><u></u></span></p>
<p><span style="FONT-SIZE: 11pt; FONT-FAMILY: Calibri,sans-serif; COLOR: rgb(31,73,125)">The thing is though, that tasklet.raise_exception() and kill() behave in a strange way when the target is already dead.  The dead tasklet is actually scheduled and the
 scheduling mechanism is put in motion, even though its “frame” is null.  It is by pure chance that things don’t break.  So, the case is handled like a regular uncaught exception.  But this is strange, since a tasklet can by definition have only one such.<u></u><u></u></span></p>
<p><span style="FONT-SIZE: 11pt; FONT-FAMILY: Calibri,sans-serif; COLOR: rgb(31,73,125)">Another reason I couldn’t emulate that behaviour with tasklet.throw(), is because it is not possible, or nice, to do so with non-immediate effect.  we shouldn’t have a
 dead tasklet in the runnable queue.<u></u><u></u></span></p>
<p><span style="FONT-SIZE: 11pt; FONT-FAMILY: Calibri,sans-serif; COLOR: rgb(31,73,125)">Hence the change.  If the tasklet is dead, then senditng it an exception is clearly an error, much as trying to rebind a dead tasklet is.  Except for the kill, which I
 think ought to be a harmless special case to allow.  <u></u><u></u></span></p>
<p><span style="FONT-SIZE: 11pt; FONT-FAMILY: Calibri,sans-serif; COLOR: rgb(31,73,125)">What do you think?</span></p>
</div>
</div>
</blockquote>
<div><br>
</div>
<div>I think I'm confused :-)</div>
<div> </div>
<div> <span style="FONT-SIZE: 11pt; FONT-FAMILY: Calibri,sans-serif; COLOR: rgb(31,73,125)"> </span></div>
<blockquote class="gmail_quote" style="PADDING-LEFT: 1ex; MARGIN: 0px 0px 0px 0.8ex; BORDER-LEFT: rgb(204,204,204) 1px solid">
<div lang="EN-GB" bgcolor="white">
<p><span style="FONT-SIZE: 11pt; FONT-FAMILY: Calibri,sans-serif; COLOR: rgb(31,73,125)">Btw,
<u></u><u></u></span></p>
<p><span style="FONT-SIZE: 11pt; FONT-FAMILY: Calibri,sans-serif; COLOR: rgb(31,73,125)">This does not preclude thinking more about how to deal with unhandled exceptions.  The current built-in behaviour is to invoke them on the mani tasklet.  But here is an
 idea:  How about defining a class method on the tasklet class, default_handler(exc, val, tb), which does this?  Then, frameworks and others can redefine this in a subclass.<u></u><u></u></span></p>
<p><span style="FONT-SIZE: 11pt; FONT-FAMILY: Calibri,sans-serif; COLOR: rgb(31,73,125)">The benefit of doing this, rather than having people wrap their main functions in try:except, is that this will work even when the tasklet hasn’t started yet, when the
 tasklet is fresh in the runnable queue.</span></p>
</div>
</blockquote>
<div>Is there a need for multiple versions of this in the same Python runtime?  I'm not convinced, and would prefer that we kept it as simple as possible.  To me, it seems a better fit to do a more standard global "stackless.set_exception_handler".  But I'd
 prefer we don't take this approach at all.</div>
<div><br>
</div>
<div>When I think about this and recall different frameworks I've written and the things which got in the way, I'd prefer we made it more generic and flexible.  Why just have an exception handler?  I think it should be a globally applied callable wrapper for
 tasklets.  But rather than manually needing to subclass a tasklet, have it applied automatically.</div>
<div><br>
</div>
<div>e.g.</div>
<div><br>
</div>
<div>def tasklet_bootstrap(f, *args, **kwargs):</div>
<div>    try:</div>
<div>        ret = f(*args, **kwargs)</div>
<div>    except:</div>
<div>        # handle</div>
<div><br>
</div>
<div>stackless.set_callable_wrapper(tasklet_bootstrap)</div>
<div><br>
</div>
<div>Cheers,</div>
<div>Richard.</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>