[Stackless] Sharing my useless module

Alejandro Castillo pyalec at gmail.com
Thu Aug 12 07:54:25 CEST 2010


Hello,

I wrote some very simple helper functions to work with tasklets, and I
thought they might be useful for someone.

It has a sleep function that uses a thread to block and avoid busy waiting.
An interactive console that spawn tasklets in the foreground or
background (using a thread).

Functions for spawning tasklets:
 * tasklet        -> creates a tasklet that you can join to and get
its return value
 * thread         -> same as 'tasklet' but creates it in another
thread. (useful for blocking tasklets)
 * interval        -> creates a tasklet that runs a function at each
specified interval of time
 * loop            -> creates a tasklet that runs a function in a loop
 * later            -> creates a tasklet scheduled to run after specified time

You can join a tasklet and get its result.
----------------------------------------------------
from useless import thread, join

def f(x):
   # do something useful

t = thread(f, x)
# ...
result = join(t)
----------------------------------------------------

Stop a running tasklet created with 'interval' or 'loop'.
----------------------------------------------------
from useless import loop, stop

def f(x):
    # do something useful

t = loop(f, x)
# ...
stop(t)
----------------------------------------------------

etc..

There's not documentation yet, but there are some simple examples on
the repository
and the main module is only 230 lines of code with docstrings.

The code is at https://bitbucket.org/pyalec/useless


Cheers,
   Alejandro Castillo



More information about the Stackless mailing list