#threadpool的內部調用參數解析 result = request.callable(*request.args, **request.kwds) # coding:utf-8 import threadpool,time time_list=[] args_list1=range(1,11) #若是要輸入元組作參數,必須把元組轉換成下面的形式 args_list2=[(i,None) for i in zip(range(1,11),range(1,11))] print args_list2 [((1, 1), None), ((2, 2), None), ((3, 3), None), ((4, 4), None), ((5, 5), None), ((6, 6), None), ((7, 7), None), ((8, 8), None), ((9, 9), None), ((10, 10), None)] #若是若是要輸入字典作參數,必須把字典轉換成下面的形式 args_list3=[(None,{"a":1,"b":2}),(None,{"a":3,"c":4}),(None,{"a":5,"b":6})] #列表 def handle_func1(a): return a #元組 def handle_func2(*a,**b): return a #字典 def handle_func3(*a,**b): return b #回調函數,會把處理函數的結果當參數 def handle_func_callback(request,results): time_list.append(results) #建立鏈接池 ,最大鏈接數5 pool=threadpool.ThreadPool(5) #處理函數handle_func*,處理函數的參數args_list*,處理函數的回調函數handle_func_callback(能夠沒有) requests1=threadpool.makeRequests(handle_func1,args_list1,handle_func_callback) requests2=threadpool.makeRequests(handle_func2,args_list2,handle_func_callback) requests3=threadpool.makeRequests(handle_func3,args_list3,handle_func_callback) #是將全部要運行多線程的請求扔進線程池 [pool.putRequest(i) for i in requests1+requests2+requests3] #鏈接池掛起,第四行是等待全部的線程完成工做後退出 pool.wait() print time_list #[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6), (7, 7), (8, 8), (9, 9), (10, 10), {'a': 1, 'b': 2}, {'a': 3, 'c': 4}, {'a': 5, 'b': 6}] import urllib2 from multiprocessing.dummy import Pool as ThreadPool def handle_func1(a): return a pool = ThreadPool(4) #對range(11)的每個數調用handle_func1,返回結果集 results = pool.map(handle_func1, range(11)) print results pool.close() pool.join()