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

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Stackless Python 101

3,102

Published on

Stackless Python presentation for Kiwi Pycon.

Stackless Python presentation for Kiwi Pycon.

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
3,102
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
77
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? <ul><li>An extended version of Python.
  • 3. Backwards compatible.
  • 4. Stable. </li></ul>
  • 5. Who uses Stackless <ul><li>CCP Games
  • 6. Who the hell knows? </li></ul>
  • 7. What Stackless provides <ul><li>Lightweight threading.
  • 8. More readable code.
  • 9. Persistence of running code. </li></ul>
  • 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 <ul><li>Allow other tasklets to run.
  • 14. Reawaken when event occurs.
  • 15. Reawaken when time has passed. </li></ul>
  • 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 <ul><li>Tasklets.
  • 22. The scheduler.
  • 23. Channels. </li></ul>
  • 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: <ul><li>Tasklet blocking.
  • 31. Tasklet communication. </li></ul>
  • 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 <ul><li>Persistence of running code
  • 38. Inter-thread channel usage.
  • 39. C. </li></ul>
  • 40. What you need to know <ul><li>You're on your own buddy... (almost) </li></ul>
  • 41. Questions Stackless website: http://www.stackless.com Stackless examples: http://code.google.com/p/stacklessexamples

×