Your SlideShare is downloading. ×
  • Like
Stackless Python 101
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Now you can save presentations on your phone or tablet

Available for both IPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Stackless Python 101

  • 2,987 views
Published

Stackless Python presentation for Kiwi Pycon.

Stackless Python presentation for Kiwi Pycon.

Published in Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
2,987
On SlideShare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
76
Comments
0
Likes
1

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

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