云萌主云应用官方论坛

标题: python多线程简单示例 [打印本页]

作者: lady-niuniu    时间: 2017-1-10 11:52
标题: python多线程简单示例
来源:http://www.open-open.com/code/view/1461224708346

  1. #!/usr/bin/env python2.7
  2. # -*- coding: utf-8 -*-

  3. import threadpool
  4. import time,random

  5. """
  6. 安装threadpool
  7. sudo easy_install threadpool
  8. """

  9. def threadpool_test(arg):
  10.     # 做一些事情
  11.     time.sleep(0.01)
  12.     return arg

  13. def print_result(request, result):
  14.     print "结果 %s %r" % (request.requestID, result)

  15. if __name__ == "__main__":
  16.     data = ['test_%d' % i for i in range(20)]

  17.     pool = threadpool.ThreadPool(5)
  18.     requests = threadpool.makeRequests(threadpool_test, data, print_result)
  19.     for req in requests:
  20.         pool.putRequest(req)

  21.     pool.wait()

  22.     print '结束!'
复制代码







欢迎光临 云萌主云应用官方论坛 (https://yunmengzhu.com/) Powered by Discuz! X3.4