2012-02-02 4 views
1

매개 변수,하지만이 코드 조각은executor.map 및 비 terating 내가 파이썬 3.2 <code>concurrent.futures</code>에 많은 쿨러 멀티 (에 스레드를 사용하는 내 스크립트를 변환하려고

 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을 읽을 때 executor.map()의 매개 변수로 람다 함수를 사용할 수 없다고 생각하고 executor.map()을 작성하려면 하나의 매개 변수 함수를 개발해야하지만 그 중 pci_idsoptions.verbose은 변수이므로 지정할 수 없습니다. 도움말 기능에서 고정 된 값으로 표시합니다.

어떤 아이디어가 필요합니까?

답변

5

피클 오류를 피하려면 모듈 또는 스크립트의 최상위 레벨에 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()]): 
+0

아, 감사합니다. – mcepl

+1

THX 나를 위해 일했습니다! 그러나 생성자 표현식이 충분할 때 목록을 인스턴스화하지 않아야하므로'executor.map (get_all_hostnames()에서 호스트에 대해 (호스트, pci_ids, options.verbose) – georg