![](/img/trans.png)
[英]How to correctly pass the generator to the function parameters of ThreadPoolExecutor Executor.map()?
[英]executor.map and non-terating parameters
我正在嘗試將腳本從使用線程轉換為更酷的多處理程序(使用python 3.2和concurrent.futures
,但是這段代碼崩潰了)
with ThreadPoolExecutor(max_workers=MAX_THREADS) as executor:
for result in executor.map(lambda h:
validate_hostname(h, pci_ids, options.verbose),
get_all_hostnames()):
我收到錯誤_pickle.PicklingError: Can't pickle <class 'function'>: attribute lookup builtins.function failed
。 在閱讀此答案時,我認為問題在於無法將lambda函數作為executor.map()
的參數,並且為了使executor.map()
我需要開發一個單參數函數,但是那些pci_ids
和options.verbose
是可變的,因此我無法在幫助功能中將它們指定為固定值。
有什么想法怎么辦?
為避免出現泡菜錯誤,必須在模塊或腳本的頂級定義函數validate
。
由於該函數將傳遞給executor.map
,因此只能接受一個參數,因此將該參數設為3元組(h, pci_ids, verbose)
。
def validate(arg):
h, pci_ids, verbose = arg
return validate_hostname(h, pci_ids, verbose)
with ThreadPoolExecutor(max_workers=MAX_THREADS) as executor:
for result in executor.map(validate, [(host, pci_ids, options.verbose)
for host in get_all_hostnames()]):
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.