views:

24

answers:

1

Stackless Experts,

I have managed to create tasklets under Stackless Python (both from the Stackless and the C side).

It seems to me that in order to create a tasklet in Stackless, you bind an arbitrary Python callable (function) to the tasklet (as well as the required parameters), so the bound callable would be run as a tasklet. However, an arbitrary callable might actually have a return value that's important to the caller. But I have yet to see a way to retrieve the return value of the bound callable running as a tasklet.

On the pure Stackless Python side, I do see a usage idiom called Micromanaging, which wraps the original function with a managing function, which in turn could capture the return value of the original function and save it somewhere for use in some other context.

Unfortunately, my special use case involves creating a tasklet from the C (C++) side, binding to a (potentially blocking) Python callable which has an important return value to be used later. It seems that writing such a Micromanaging function on the C side is not very feasible, since I haven't found a way to turn a C function into a PyObject callable dynamically (not involving module table initialization, etc.), and using static stateless C function (I would assume the prototype has to be PyObject* (PyObject*, PyObject*)) is generally a bad idea under C++ world anyways.

The Stackless C API also seems not containing a proper function to retrieve the return value of a tasklet. Is it the only option that I have to write above mentioned Micromanaging function (could be stateful) in Python, and provide a way to retrieve the return value saved somewhere (in a stateful fashion, i.e. not using global variables)? Or there could be other options I could possibly explore?

Thank you very much,

Lin

P.S. I understand that on the C and operating system level programming, the return value of a thread function is only meaningful for exit code, and thread functions would have a strict function prototype, i.e. C functions have to abide by some strict rules to become runnable as/by a thread. And now we are talking about Python :)

A: 

From Stackless.com:

"... But I have yet to see a way to retrieve the return value of the bound callable running as a tasklet. ..."

I do not know that there is one.

"...important return value to be used later. It seems that writing such a Micromanaging function on the C side is not very feasible, since I haven't ..."

The simplest way to do this would be to define the wrapping Python function code in C as a string, and to compile it into a function object that you can run as a tasklet. An example Python function follows, the C part I leave to you.

e.g.

  def RunFunctionAndGetResult(chan, func, *args, **kwargs):
      chan.send(func(*args, **kwargs))

Cheers,

Richard.

Lin
If you have the leisure to define the above Python function in one of your Python modules, then you don't have to compile the above function in the format of a C string; on the C side, you can just get the reference to that function and wrap it to a tasklet!
Lin