Source code for geventtools

# -*- coding: utf-8 -*-
import gevent
import urllib2
from datetime import datetime
from gevent.pool import Pool

from gevent import monkey
monkey.patch_all()


[docs]def gevent_pool(func, spawn_list, pool_size=5): ''' gevent_pool :param object func: main run function :param list spawn_list: a list of :func:`midfunc` :param int pool_size: gevent pool concurrency ''' pool = Pool(pool_size) gevent_spawn_list = [] for args, kwargs in spawn_list: gevent_spawn_list.append(pool.spawn(func, *args, **kwargs)) gevent.joinall(gevent_spawn_list) return gevent_spawn_list
[docs]def midfunc(*args, **kwargs): ''' midfunc :rtype: tuple :returns: (args, kwargs) ''' return args, kwargs
if __name__ == '__main__': def fetch_data(url): print datetime.now(), 'Start', url result = urllib2.urlopen(url) print datetime.now(), 'Fetch OK', url print url, result return result.info() url = [ midfunc('http://www.google.com.tw/'), midfunc('http://toomore.net/'), midfunc('http://pinkoi.toomore.net/'), midfunc(url='http://www.pinkoi.com/'), ] t1 = datetime.now() result = gevent_pool(fetch_data, url*20, 20) for i in result: print i.get() print result print datetime.now() - t1