Skip to content

Instantly share code, notes, and snippets.

@flyer103
Created December 5, 2013 06:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flyer103/7801236 to your computer and use it in GitHub Desktop.
Save flyer103/7801236 to your computer and use it in GitHub Desktop.
验证自己对主线程到 hub greenlet 的切换和 greenlets 间的切换
#!/usr/bin/env python2.7
#coding: utf-8
import gevent
class TestMainCo(object):
def __init__(self, mtimeout=2, gtimeout=3, numtasks=10):
self.mtimeout = mtimeout
self.gtimeout = gtimeout
self.numtasks = numtasks
def run(self):
tasks = [gevent.spawn(self._task, i) for i in xrange(self.numtasks)]
gevent.joinall(tasks, timeout=self.mtimeout)
print('Tasks done!')
def _task(self, pid):
print('Task %d begin' % (pid,))
gevent.sleep(self.gtimeout)
print('Task %d done' % (pid,))
if __name__ == '__main__':
test = TestMainCo()
test.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment