Stackless Python 101

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    1 Group

    Stackless Python 101 - Presentation Transcript

    1. Stackless Python 101 An advanced level presentation in 20 minutes Kiwi PyCon 9 th November 2009 Richard Tew [email_address] www.stackless.com
    2. What is Stackless?
      • An extended version of Python.
      • Backwards compatible.
      • Stable.
    3. Who uses Stackless
      • CCP Games
      • Who the hell knows?
    4. What Stackless provides
      • Lightweight threading.
      • More readable code.
      • Persistence of running code.
    5. Lightweight threading?
    6. The shared thread
    7. More readable code def Function_1(): Function_2() def Function_2(): Current_Function() def Current_Function(): # whatever...
    8. Reasons to block
      • Allow other tasklets to run.
      • Reawaken when event occurs.
      • Reawaken when time has passed.
    9. 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)
    10. stacklesssocket.py Third party library. Monkey patches in place of Python sockets import stacklesssocket stacklesssocket.install()
    11. 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)
    12. Where's the readability? No callbacks. No boilerplate. Just a function call. do_something_i_dont_care_if_you_block()
    13. Comparison: generators Generators block to the calling function. Not the scheduler. http://www.dabeaz.com/coroutines
    14. Using Stackless
      • Tasklets.
      • The scheduler.
      • Channels.
    15. The scheduler stackless.run(...)
    16. Scheduling cooperatively Tasklets choose when to block stackless.schedule() channels... Run the scheduler until it is empty. stackless.run()
    17. Scheduling preemptively while 1: interruptedTasklet = stackless.run(100) interruptedTasklet.insert() # Do other stuff.
    18. Tasklets stackless.tasklet(print_args)(1, 2, x=“test“)
    19. Tasklets and threads
    20. Tasklet yielding def print_args(*args, **kwargs): while 1: print 'args', args print 'kwargs', kwargs stackless.schedule()
    21. Channels Used for:
      • Tasklet blocking.
      • Tasklet communication.
    22. Channel operations Sending: channel.send(value) Receiving: value = channel.receive()
    23. One blocking tasklet def send_value(channel, value): channel.send(value) channel = stackless.channel() stackless.tasklet(send_value)(channel, 1) stackless.run()
    24. 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()
    25. 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()
    26. 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)
    27. What I have not covered
      • Persistence of running code
      • Inter-thread channel usage.
      • C.
    28. What you need to know
      • You're on your own buddy... (almost)
    29. Questions Stackless website: http://www.stackless.com Stackless examples: http://code.google.com/p/stacklessexamples
    SlideShare Zeitgeist 2009

    + guest162fd90guest162fd90 Nominate

    custom

    373 views, 0 favs, 0 embeds more stats

    Stackless Python presentation for Kiwi Pycon.

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 373
      • 373 on SlideShare
      • 0 from embeds
    • Comments 0
    • Favorites 0
    • Downloads 11
    Most viewed embeds

    more

    All embeds

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories

    Tags

    Groups / Events