在 python 中,我有一個需要上傳的檔案串列。
示例代碼可能如下所示:
for path, key in upload_list:
upload_function.upload_file(path, key)
我怎樣才能多執行緒這樣的任務?
我已經遇到過多個解決方案,例如行程回圈,請參閱:如何在 Python 中使用多執行緒函式? 但對我來說,這些似乎有點矯枉過正,只是為了處理要上傳的檔案串列。有沒有更聰明的方法?
uj5u.com熱心網友回復:
這并不復雜ThreadPoolExecutor
def upload_files(upload_list):
def __upload(val):
upload_function.upload_file(*val)
with ThreadPoolExecutor() as executor:
executor.map(__upload, upload_list)
upload_files(upload_list)
如果您可以修改upload_function.upload_file以接收keyandpath作為一個引數,則該功能可以簡化為
upload_list = [...]
with ThreadPoolExecutor() as executor:
executor.map(upload_function.upload_file, upload_list)
executor.map(__upload, upload_list)將__upload使用一個子串列upload_list作為引數運行該函式,并且它將(幾乎)同時運行所有這些子串列。
要控制執行緒數,您可以使用max_workers引數
with ThreadPoolExecutor(max_workers=len(upload_list)) as executor:
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/474775.html
上一篇:將pandasql輸出分配給DataFrame中的新列
下一篇:打開多個json檔案并單獨讀取
