Skip to content

Instantly share code, notes, and snippets.

@flyer103
Created December 5, 2013 06:48
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/7801167 to your computer and use it in GitHub Desktop.
Save flyer103/7801167 to your computer and use it in GitHub Desktop.
验证自己对 gevent 中 greenlets 调度顺序的理解
#!/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