Created
December 5, 2013 06:48
-
-
Save flyer103/7801167 to your computer and use it in GitHub Desktop.
验证自己对 gevent 中 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/python2.7 | |
# coding: utf-8 | |
import gevent | |
class TestSchedule(object): | |
def __init__(self, timeout=1, numtasks=10): | |
self.timeout = timeout | |
self.numtasks = numtasks | |
def run(self): | |
tasks = [gevent.spawn(self._task, i) for i in xrange(self.numtasks)] | |
gevent.joinall(tasks) | |
print('All tasks are done!') | |
def _task(self, pid): | |
print('Task %d begin, wanting to sleep......' % (pid,)) | |
gevent.sleep(self.timeout) | |
print('Task %d has waken up' % (pid,)) | |
if __name__ == '__main__': | |
test = TestSchedule() | |
test.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment