7

python多线程简单示例

open_run 10年前发布 | 64K 次阅读 Python ThreadPool
#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-
import threadpool 
import time,random 
"""
安装threadpool
sudo easy_install threadpool
"""
def threadpool_test(arg): 
 # 做一些事情
 time.sleep(0.01) 
 return arg 
def print_result(request, result): 
 print "结果 %s %r" % (request.requestID, result) 
if __name__ == "__main__":
 data = ['test_%d' % i for i in range(20)] 
 pool = threadpool.ThreadPool(5) 
 requests = threadpool.makeRequests(threadpool_test, data, print_result) 
 for req in requests:
 pool.putRequest(req) 
 pool.wait() 
 print '结束!'
本文由用户 open_run 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
转载本站原创文章,请注明出处,并保留原始链接、图片水印。
本站是一个以用户分享为主的开源技术平台,欢迎各类分享!

AltStyle によって変換されたページ (->オリジナル) /