Created
December 5, 2013 06:56
-
-
Save flyer103/7801236 to your computer and use it in GitHub Desktop.
验证自己对主线程到 hub greenlet 的切换和 greenlets 间的切换
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
#!/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