线程池和协程池

线程池

from concurrent.futures import ThreadPoolExecutor
executor = ThreadPoolExecutor(3)
def f(a, b):
	  print('f', a, b)
	  return a ** b
executor.submit(f, 2, 3)
future.result()
executor.map(f, [2, 3, 5], [4, 5, 6])

def f(a, b):
     print('f', a, b)
     time.sleep(10)
     return a ** b
executor.map(f, [2, 3, 4, 5, 6], [4, 5, 6, 7, 8])

协程池

from gevent import monkey
monkey.patch_all()
  
import urllib2
from gevent.pool import Pool
  
def download(url):
    return urllib2.urlopen(url).read()

  
if __name__ == '__main__':
    urls = ['http://httpbin.org/get'] * 100
    pool = Pool(20)
    print pool.map(download, urls)

你可能感兴趣的:(python基础)