2012-02-02 15 views
1

我想我的腳本使用線程轉換爲更涼爽多(與Python 3.2和concurrent.futures,但這段代碼崩潰executor.map和非terating參數

 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。當閱讀this answer我認爲問題是,它是不可能有lambda表達式爲executor.map()參數,併爲了使executor.map()我需要開發一個參數的功能,但這些pci_idsoptions.verbose是可變的,所以我不能指定它們作爲幫助功能中的固定值。

任何想法該怎麼辦?

回答

5

爲了避免泡菜的錯誤,你必須定義功能,validate,在模塊或腳本的頂層。

由於該函數被傳遞給executor.map它只能帶一個參數,所以讓該參數爲三元組(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()]): 
+0

哦,對了,謝謝你! – mcepl 2012-02-03 07:31:37

+1

爲我工作,T​​HX!但是應該避免在生成器表達式足夠時實例化列表,因此您應該更改爲'executor.map(validate,((host,pci_ids,options.verbose)for get_all_hostnames())) – georg 2016-02-10 21:23:27