Created
December 22, 2011 20:31
-
-
Save fabiokung/1511736 to your computer and use it in GitHub Desktop.
generators as fibers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# python 2.7.x | |
class Fiber: | |
def __init__(self, callback): | |
self._callback = callback() | |
def resume(self): | |
return self._callback.next() | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def work(): | |
yield 1 | |
yield 2 | |
f = Fiber(work) | |
print f.resume() | |
print f.resume() | |
# PS1: almost same API as http://ruby-doc.org/core/Fiber.html | |
# PS2: http://www.python.org/dev/peps/pep-0342/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment