Stackless Python 101 An advanced level presentation in 20 minutes Kiwi PyCon 9 th  November 2009 Richard Tew [email_address] www.stackless.com
What is Stackless? An extended version of Python.
Backwards compatible.
Stable.
Who uses Stackless CCP Games
Who the hell knows?
What Stackless provides Lightweight threading.
More readable code.
Persistence of running code.
Lightweight threading?
The shared thread
More readable code def Function_1(): Function_2() def Function_2(): Current_Function() def Current_Function(): # whatever...
Reasons to block Allow other tasklets to run.
Reawaken when event occurs.
Reawaken when time has passed.
Example: Python sockets def Function_1(self): self.socket = socket(AF_INET, SOCK_STREAM) ... self.Function_2() def Blocking_Function(self): return self.socket.recv(1000)
stacklesssocket.py Third party library. Monkey patches in place of Python sockets import stacklesssocket stacklesssocket.install()
Example: Stackless sockets def Function_1(self): self.socket = socket(AF_INET, SOCK_STREAM) ... self.Function_2() def Blocking_Function(self): return self.socket.recv(1000)
Where's the readability? No callbacks. No boilerplate. Just a function call. do_something_i_dont_care_if_you_block()
Comparison: generators Generators block to the calling function. Not the scheduler. http://www.dabeaz.com/coroutines
Using Stackless Tasklets.
The scheduler.
Channels.
The scheduler stackless.run(...)
Scheduling cooperatively Tasklets choose when to block stackless.schedule() channels... Run the scheduler until it is empty. stackless.run()
Scheduling preemptively while 1: interruptedTasklet = stackless.run(100) interruptedTasklet.insert() # Do other stuff.
Tasklets stackless.tasklet(print_args)(1, 2, x=“test“)
Tasklets and threads
Tasklet yielding def print_args(*args, **kwargs): while 1: print 'args', args print 'kwargs', kwargs stackless.schedule()

Stackless Python 101