From:

Stackless notes #1

Creating a tasklet

When you create a tasklet, it is automatically scheduled. This places it at the end of the list of scheduled tasklets, and out of all the tasklets currently waiting to run, it will get its turn last.

Scheduling a new tasklet:
>>> def f():
... pass
...
>>> stackless.tasklet(f)()
There is no need to hold a reference to the tasklet to keep it alive. The scheduler holds a reference to all scheduled tasklets.

Binding a callable to a tasklet:
>>> def f(arg, kwarg=None):
... pass
...
>>> t = stackless.tasklet(f)
>>> t.scheduled
False
>>> t.alive
False
Supplying the parameters for the callable:
>>> t = stackless.tasklet(f)
>>> t(1, kwarg=2)
<stackless.tasklet object="" at="" 0x01b031b0="">
>>> t.alive
True
>>> t.scheduled
True
Running the scheduler

Once started, it will continue to run until it has no scheduled tasklets remaining.
>>> def f():
... print "scheduled once"
... stackless.schedule()
... print "scheduled twice"
...
>>> stackless.tasklet(f)()
<stackless.tasklet object at 0x01B03170>
>>> stackless.run()
scheduled once
scheduled twice
>>>
If a tasklet never exits, the scheduler will continue to run forever. This would happen with the following function:
>>> def f():
... while True:
... stackless.schedule()
...
>>>
Running the scheduler normally

As already shown, the normal way to use Stackless Python, is to create your tasklets and then to run the scheduler until they have all either blocked out of it, or run to completion.

An example function to schedule as a tasklet:
def SomeFunction():
while not shouldExit:
# Do whatever it is this tasklet is supposed to do.
pass
# We need to yield to allow the other scheduled tasklets to run.
stackless.schedule()
Create and schedule the tasklet:
stackless.tasklet(SomeFunction)()
Run the scheduler until there are no remaining scheduled tasklets:
stackless.run()
Using this approach, Stackless is a framework and it drives the running of your application. If your application needs to do something, then it needs to create a tasklet to run alongside the others in the scheduler.

Running the scheduler in a more flexible way

It is often less constraining not to have to shape your program to fit inside a framework. This is not how Stackless was used above, in the normal manner. But it can be done by approaching things a little bit differently.

This is what we do in EVE Online, as Kristján Valur Jónsson described in his presentation at PyCon 2006. The approach is described in the Stackless Python idioms wiki page, as the 'Being Nice' idiom.

In a nutshell, rather than yielding your tasklets to the end of the scheduled tasklet list, you yield them onto a channel and out of the scheduler. This is the 'BeNice' function:
def LoopingFunction():
BeNice()
The scheduler will then exit after each tasklet has been run once. Then when you next get to the point when you can run the scheduler again, you first wake up each tasklet on your yield channel, then run the scheduler.
def MainLoop():
while not exit:
RunNiceTasklets()
stackless.run()
The Stackless 'schedule' method should not be used to yield any more, otherwise you will block on the scheduler running until the tasklets which use it are blocked off the scheduler in some other way, or have exited.

An example implementation of this is provided in the uthreadmodule.
import uthread

def LoopingFunction():
uthread.BeNice()

if __name__ == "__main__":
uthread.Run()
Of course, as is, it is not much use. You need to integrate your logic into the loop within your own version of the 'Run' function. Otherwise, you are not much better off than just using Stackless normally.

Related Articles

Relatd Projects

outofoffice

This program answer to your email with the message you have set when you are out of your office. It's like the Outlook's Out of office function but you don't need Outlook or Exchange...
guice-1.0
Put simply, Guice alleviates the need for factories and the use of new in your Java code. Think of Guice's @Inject as the new new. You will still need to write factories in some cases, but your code will not depend directly on them. Your code will be easier to change, unit test and reuse in other contexts. Guice embraces Java's type safe nature, especially when it comes to features introduced in Java 5 such as generics and annotations. You might think of Guice as filling in missing features for core Java. Ideally, the language itself would provide most of the same features, but until such a language comes along, we have Guice. Guice helps you design better APIs, and the Guice API itself sets a good example. Guice is not a kitchen sink. We justify each feature with at least three use cases. When in doubt, we leave it out. We build general functionality which enables you to extend Guice rather than adding every feature to the core framework. Guice aims to make development and debugging easier and faster, not harder and slower. In that vein, Guice steers clear of surprises and magic. You should be able to understand code with or without tools, though tools can make things even easier. When errors do occur, Guice goes the extra mile to generate helpful messages.
ojob
A flexible job scheduler for the distributed environment. Schedule a job on any server - from any server. Create dependencies and trigger alerts across the network. Monitor and control it all from your desktop.
jadvisor
JAdvisor is a class scheduler, course planner, and course search program. It also allows college students to view their schedules graphically and create an optimal schedule. Adapters are used to customize JAdvisor for your particular school.
wiideocenter
Wiideo Center is a small application which can transcode and stream all videos from your PC directly to your Nintendo Wii Console (Internet Channel required!). Its an all-in-one package and you don't have to install anything. Just unzip and run!
Apache Commons Launch
The Launcher Component is designed to be a cross platform Java application launcher. The original Java classes come from the Tomcat 4.0 project. Commons-launcher eliminates the need for a batch or shell script to launch a Java class. Some situations where elimination of a batch or shell script may be desirable are: * You want to avoid having to determining where certain application paths are e.g. your application's home directory, etc. Determining this dynamically in a Windows batch scripts is very tricky on some versions of Windows or when softlinks are used on Unix platforms. * You want to avoid having to handle native file and path separators or native path quoting issues. * You need to enforce certain system properties e.g. java.endorsed.dirs when running with JDK 1.4. * You want to allow users to pass in custom JVM arguments or system properties without having to parse and reorder arguments in your script. This can be tricky and/or messy in batch and shell scripts. * You want to bootstrap system properties from a configuration file instead hard-coding them in your batch and shell scripts. * You want to provide localized error messages which is very tricky to do in batch and shell scripts.
dev-hell
Otherwise known as 'The Great Game-Making Game', it will be an browser-based online game where players have to battle it out to become the best games producers. The game will feature lots of comedy as well, just to keep you happy!
Apache Commons JDBC Utility
DbUtils is a small set of classes designed to make working with JDBC easier. JDBC resource cleanup code is mundane, error prone work so these classes abstract out all of the cleanup tasks from your code leaving you with what you really wanted to do with JDBC in the first place: query and update data. Some of the advantages of using DbUtils are: * No possibility for resource leaks. Correct JDBC coding isn't difficult but it is time-consuming and tedious. This often leads to connection leaks that may be difficult to track down. * Cleaner, clearer persistence code. The amount of code needed to persist data in a database is drastically reduced. The remaining code clearly expresses your intention without being cluttered with resource cleanup. * Automatically populate JavaBean properties from ResultSets. You don't need to manually copy column values into bean instances by calling setter methods. Each row of the ResultSet can be represented by one fully populated bean instance.
progdvbtimer
ProgDVB Timer is a tool to schedule ProgDVB records. It generates (windows) scheduled tasks to start and stop records with ProgDVB automatically. Some options are optional available, e.g. mute, minimized or exit ProgDVB after record. Channels can be sele
w3s
A personal webspyder. ¿Do you want to explore a website for things you're interested in but you don't have the time to do it? W3s does it for you and shows you the results in your Internet web browser.