• Like
  • Save
Stackless Python 101
Upcoming SlideShare
Loading in...5
×
 

Stackless Python 101

on

  • 4,657 views

Stackless Python presentation for Kiwi Pycon.

Stackless Python presentation for Kiwi Pycon.

Statistics

Views

Total Views
4,657
Views on SlideShare
4,638
Embed Views
19

Actions

Likes
1
Downloads
73
Comments
0

1 Embed 19

http://www.slideshare.net 19

Accessibility

Categories

Upload Details

Uploaded via as OpenOffice

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Stackless Python 101 Stackless Python 101 Presentation Transcript

    • 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()
    • Channels Used for:
      • Tasklet blocking.
      • Tasklet communication.
    • Channel operations Sending: channel.send(value) Receiving: value = channel.receive()
    • One blocking tasklet def send_value(channel, value): channel.send(value) channel = stackless.channel() stackless.tasklet(send_value)(channel, 1) stackless.run()
    • Multiple blocking tasklets def send_value(channel, value): channel.send(value) channel = stackless.channel() stackless.tasklet(send_value)(channel, 1) stackless.tasklet(send_value)(channel, 2) stackless.tasklet(send_value)(channel, 3) stackless.run()
    • Emptying the channel def empty_channel(channel): print 'tasklets waiting:', channel.balance while channel.balance > 0: value = channel.receive() print 'received', value stackless.tasklet(empty_channel)(channel) stackless.run()
    • Idiom: Blocking out of the scheduler rapture_channel = stackless.channel() def wait_for_rapture(): rapture_channel.receive() def call_me_when_rapture_occurs(): while rapture_channel.balance < 0: rapture_channel.send(None)
    • What I have not covered
      • Persistence of running code
      • Inter-thread channel usage.
      • C.
    • What you need to know
      • You're on your own buddy... (almost)
    • Questions Stackless website: http://www.stackless.com Stackless examples: http://code.google.com/p/stacklessexamples