python多線程簡單示例

open_run 8年前發布 | 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 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!