[Stackless] Access to local variables of a task from tasklet

Richard Tew richard.m.tew at gmail.com
Sat Jun 20 01:03:06 CEST 2009

On Sat, Jun 20, 2009 at 10:40 AM, A.M.<aelorean at yahoo.com> wrote:
> Thank you -- I will attempt to do so.
> My original question had to do with having tasklets return an actual value
> using the C API ....I've found a lot of examples of how to use tasklets with
> the C API and I understand everything fairly well.  However, I cannot find a
> single example of how you'd use a tasklet to run a task that returns a
> value.

Tasklets are like threads, they wrap code to be run.  You can't call
them directly like a function and get a return value from them.

You can do this indirectly using channels as indicated in earlier
emails.  One primitive used at C.C.P. was the 'parallel' function [1]
in the 'uthread' module.  In this, you'd specify one or more functions
to run as tasklets with the arguments they should take.

[1] http://code.google.com/p/stacklessexamples/source/browse/trunk/libraries/uthread-ccp/uthread.py#818

So given you had:

  def FuncA(v1, v2):
       return dbconn.exec("SELECT * FROM TableA WHERE v1=%s AND v2=%s"
% (v1, v2))

  def FuncB(v1, v2):
       return dbconn.exec("SELECT * FROM TableB WHERE v1=%s AND v2=%s"
% (v1, v2))

You could do

  def SomeTaskletThatSavesTimeByWaitingForMultipleThingsInParallel():
      rowA, rowB = uthread.parallel((
          (FuncA, (1, 2)),
          (FuncB, (2, 3)),
      # Do stuff with rowA and rowB.

Now, I wouldn't advise anyone use the uthread library.  The original
uthread library catered for the fact Stackless was based on
continuations and added a range of supporting code to make
microthreading on top of that straightforward.  The C.C.P. version is
just a range of helper functions accrued during the course of EVE
Online development.  Better would be to understand how to extract the
types of functionality it provides which are of interest, and to adopt
them into supporting resources in your own application.

The primary advantage of the pattern of pseudo-code given above is
that DB operations like other types of IO in the EVE Online framework
are asynchronous.  Any tasklet which does one is blocked until the
result comes in then awakened (again, via channels).  Now, you can
call the DB operations one after the other and block as long as it
takes to do each one after the other. Or you can do them all in
separate tasklets/threads of execution and wait only as long as the
slowest one takes as they all proceed at the same time.

> But, maybe I can utilize some of the examples you all gave with regard to
> channels with python and see what I can come up with.  At least I'll have
> something for you all to look through and let me know where my logic is
> flawed.
> Thanks again!

More information about the Stackless mailing list