[Stackless] how nonblocking send and receive tasklet's message using c api?

Richard Tew richard.m.tew at gmail.com
Sat Mar 24 07:24:02 CET 2012


On Sat, Mar 24, 2012 at 4:44 PM, Simon Liu <simohayha.bobo at gmail.com> wrote:
> I am using stackless c api(stackless_api.h), but I have some question:
>
> when I new a tasklet and run it, I lost control this new tasklet, example I
> want know this new tasklet whether dead or done. afterwards I try use
> channel, but in my main tasklet , everything must be nonblocking.  so
> how nonblocking send and receive tasklet's message? like erlang's
> spawn_link.

I don't know anything about erlang's spawn_link.

If you wrap the callable you pass into the tasklet, with an outer
function, you can detect the entrance and exit of the tasklet by
having that outer function.  In Python you would do it like this:

exited_tasklets = []

def create_tasklet(f, *args, **kwargs):
    def wrap_tasklet():
        try:
            f(*args, **kwargs)
        finally:
            exited_tasklets.append(self)
    return stackless.tasklet(wrap_tasklet)()

With a little more effort, you could do something similar when using
the C API.  There are most likely threads in the mailing list archives
somewhere which go into this in detail, but I don't have the knowledge
of the links offhand.

Another way with regard to your mention of channels, which I would
never use myself, is to poll the channel in the main tasklet.  You can
check if a channel has something waiting on it, by checking it's
"balance".  If there is a balance, you can then receive.  Which
tasklet blocks and which gets immediately rescheduled can be altered
by the channel's "preference" which would be useful to ensure that the
main tasklet simply fetches the sent value and .

void PyChannel_SetPreference(PyChannelObject *self, int val);
int PyChannel_GetBalance(PyChannelObject *self);

Cheers,
Richard.



More information about the Stackless mailing list