簡體   English   中英

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_idsoptions.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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM