[Stackless] tracing stackless events

Andrew Dalke dalke at dalkescientific.com
Mon Dec 10 02:22:56 CET 2007

I've been experimenting with how to trace stackless events.  The ones  
I'm most interested in are tasklet creation and destruction, context  
switches between tasklets, and channel send/receive.

With clues from the mailing list archive I found set_channel_callback  
and set_schedule_callback.  These do most, but not everything I want.

Some things I noticed:

   - there's no way to know when a tasklet was created.  I worked  
around it by reaching into the stackless module and wrapping 'tasklet'.

   - the documentation for set_schedule_callback says the callback takes
     two arguments, (prev, next)

          When a tasklet is dying, next is None.

      I never got a callback for this case.  My workaround will be to  
use the weak reference notification event to watch the tasklet.

   - the code for calling the channel callback silently swallows any  
exceptions raised during the callback.  It should at least dump the  
traceback to stderr.

   - I would like to assign a name and perhaps a group name or other  
properties to the tasklet and channel.  That makes it easier to  
debug.  But they are C types and I can't.  I had to use a weak  
dictionary instead.

I've attached my code so far, which computes 4! recursively, where  
each recursive call is through a new tasklet.  Here's the output

* main created factorial(3)
tasklet main receive via <factorial(3) result> (will block)
context switch main -> factorial(3)
* factorial(3) created factorial(2)
tasklet factorial(3) receive via <factorial(2) result> (will block)
context switch factorial(3) -> factorial(2)
* factorial(2) created factorial(1)
tasklet factorial(2) receive via <factorial(1) result> (will block)
context switch factorial(2) -> factorial(1)
tasklet factorial(1) send 1 via <factorial(1) result>
    factorial(2) is queued
context switch factorial(1) -> factorial(2)
tasklet factorial(2) send 2 via <factorial(2) result>
    factorial(3) is queued
context switch factorial(2) -> factorial(3)
tasklet factorial(3) send 6 via <factorial(3) result>
    main is queued
context switch factorial(3) -> main
fact = 24
Remaining objects:
[(<stackless.tasklet object at 0x61bd70>, 'main')]
outside of main
outside of main

I don't know why the "outside of main" is called twice.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: trace.py
Type: text/x-python-script
Size: 3976 bytes
Desc: not available
URL: <http://www.stackless.com/pipermail/stackless/attachments/20071210/1f140a91/attachment.bin>
-------------- next part --------------

For fun, try changing
     result_ch.preference = 1
     result_ch.preference = -1

and see that the intermediate tasklets aren't removed until after the  
factorial is computed.  With it ==1 they go to completion and are  
removed quickly.

				dalke at dalkescientific.com

More information about the Stackless mailing list