Created
September 30, 2013 17:25
-
-
Save simplyvikram/6767155 to your computer and use it in GitHub Desktop.
Testing Greenlet creation
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
from gevent import Greenlet | |
import gevent | |
class MyNoopGreenlet(Greenlet): | |
def __init__(self, number, seconds): | |
Greenlet.__init__(self) | |
self.number = number | |
self.seconds = seconds | |
print "Initialized greenlet with number:%i seconds:%i" % \ | |
(self.number, self.seconds) | |
def _run(self): | |
print "Entered run in greenlet number %i" % (self.number,) | |
gevent.sleep(self.seconds) | |
print "Exiting greenlet number %i" % (self.number,) | |
def __str__(self): | |
return 'MyNoopGreenlet(number:%s, seconds:%s)' \ | |
% (self.number, self.seconds) | |
if __name__ == "__main__": | |
greenlets = map(lambda number, seconds: MyNoopGreenlet(number, seconds), | |
[1, 2, 3], | |
[3, 3, 3]) | |
gevent.sleep(0) | |
print "About to start greenlets. This would not execute them though," \ | |
" till main greenlet sleeps" | |
map(lambda g: g.start(), greenlets) | |
print "Started all greenlets. Will now sleep main greenlet for 0 seconds " \ | |
"to allow child greenlets to run" | |
gevent.sleep(0) | |
print " Will now block till child greenlets finish executing" | |
gevent.joinall(greenlets) | |
print " All child greenlets have finished executing" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment