Skip to content

Instantly share code, notes, and snippets.

@simplyvikram
Created September 30, 2013 17:25
Show Gist options
  • Save simplyvikram/6767155 to your computer and use it in GitHub Desktop.
Save simplyvikram/6767155 to your computer and use it in GitHub Desktop.
Testing Greenlet creation
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