python多线程模块threadpool简单使用

    python实现线程池通常使用threading或thread模块来编写,现在已经有了threadpool模块来实现线程池。

英文文档见:http://www.chrisarndt.de/projects/threadpool/

中文文档见:http://gashero.yeax.com/?p=44

现给出一个简易的使用threadpool模块来实现线程池的例子:

 

  
  
  
  
  1. #!/usr/bin/env python 
  2. import threadpool 
  3. import time,random 
  4.  
  5. def hello(str): 
  6.     time.sleep(2
  7.     return str 
  8.  
  9. def print_result(request, result): 
  10.     print "the result is %s %r" % (request.requestID, result) 
  11.  
  12. data = [random.randint(1,10for i in range(20)] 
  13.  
  14. pool = threadpool.ThreadPool(5
  15. requests = threadpool.makeRequests(hello, data, print_result) 
  16. [pool.putRequest(req) for req in requests] 
  17. pool.wait() 

 

你可能感兴趣的:(thread,多线程,python,模块,英文)