0
Stackless Python 101 An advanced level presentation in 20 minutes Kiwi PyCon 9 th  November 2009 Richard Tew [email_addres...
What is Stackless? <ul><li>An extended version of Python.
Backwards compatible.
Stable. </li></ul>
Who uses Stackless <ul><li>CCP Games
Who the hell knows? </li></ul>
What Stackless provides <ul><li>Lightweight threading.
More readable code.
Persistence of running code. </li></ul>
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 <ul><li>Allow other tasklets to run.
Reawaken when event occurs.
Reawaken when time has passed. </li></ul>
Example: Python sockets def Function_1(self): self.socket = socket(AF_INET, SOCK_STREAM) ... self.Function_2() def Blockin...
stacklesssocket.py Third party library. Monkey patches in place of Python sockets import stacklesssocket stacklesssocket.i...
Example: Stackless sockets def Function_1(self): self.socket = socket(AF_INET, SOCK_STREAM) ... self.Function_2() def Bloc...
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 <ul><li>Tasklets.
The scheduler.
Channels. </li></ul>
The scheduler stackless.run(...)
Scheduling cooperatively Tasklets choose when to block stackless.schedule() channels... Run the scheduler until it is empt...
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()
Upcoming SlideShare
Loading in...5
×

Stackless Python 101

3,125

Published on

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,125
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
78
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Transcript of "Stackless Python 101"

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

    Clipping is a handy way to collect important slides you want to go back to later.

×