之前透過threading.thread()進行了助力介面的多執行緒並發,但是這個針對並發數量較少的時候比較好用,如果並發數量多,除了線程包協程這種處理方式的情況下,我們還可以採用線程池的方法。
線程池的實作通俗講就是把所有的任務放在了訊息佇列裡,開啟多個執行緒後執行線程,但執行緒執行結束後不會中斷執行緒任務,會從訊息佇列內繼續取得執行緒任務進行執行緒執行,這樣執行緒池就比多執行緒操作節省了許多創建執行緒與關閉執行緒的步驟,節約大部分資源與時間。
import concurrent.futures
ThreadPoolExecutor 內有兩種執行緒池方法map()與submit()今天先說map()方法
with concurrent.futures.ThreadPoolExecutor() as pool: res = pool.map(craw, uid_list) print(res)
map()
內craw為方法名,這裡方法命不帶()
uid_list
為方法參數,map()方法內需要傳遞list資料型別
def test_case_09(self): """5000用户并发助力""" # 通过yaml配置文件封装方法 获取uid_list uid_list = YamlHandler(YamlThePath().number_new).get_uid_list() # add_ticket获取5000账号登陆状态 with concurrent.futures.ThreadPoolExecutor() as pool: pool.map(AccountAccess().add_ticket, uid_list) # 5000账号线程池方法助力用户 with concurrent.futures.ThreadPoolExecutor() as pool: pool.map(PreheatMethod().help, [(uid, self.A, 1) for uid in uid_list]) # 获取用户被助力次数 response = PreheatMethod().init(self.A) print(f"当前用户被助力次数 :{response['data']['userInfo']['helpedCount']}次")
def add_ticket(self, uid): """ 获取单独用户t票 :param uid: 单独用户uid :return: """ self.data['url'] = ApiAddress().get_ticket self.data['host'] = ApiAddress().host self.params['uid'] = str(uid) self.params['type'] = 0 self.data['params'] = json.dumps(self.params) res = r().post(url=ApiAddress().ticket, data=self.data) print(f'获取t票结果:{uid}{res}') return uid
def help(self, agrs): """ 助力用户 :param agrs: uid:当前用户uid to_uid:助力用户uid count:助力次数 :return: """ uid, to_uid, count = agrs self.attrs['toUid'] = str(to_uid) self.attrs['count'] = count response = r().response(uid, self.code, "help", **self.attrs) logger.info(f'help response uid:{uid} to_uid:{to_uid}\n{response}') return response
with concurrent.futures.ThreadPoolExecutor() as pool: pool.map(PreheatMethod().help, [(uid, self.A, 1) for uid in uid_list])
[(uid, self.A, 1) for uid in uid_list]
以上是python之怎麼使用執行緒池map()方法傳遞多參數list的詳細內容。更多資訊請關注PHP中文網其他相關文章!