How does ThreadPoolExecutor().map differ from ThreadPoolExecutor().submit?

后端 未结 4 1231
耶瑟儿~
耶瑟儿~ 2021-01-30 06:45

I was just very confused by some code that I wrote. I was surprised to discover that:

with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor:
             


        
4条回答
  •  隐瞒了意图╮
    2021-01-30 07:10

    if you use concurrent.futures.as_completed, you can handle the exception for each function.

    import concurrent.futures
    iterable = [1,2,3,4,6,7,8,9,10]
    
    def f(x):
        if x == 2:
            raise Exception('x')
        return x
    
    with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor:
        result_futures = list(map(lambda x: executor.submit(f, x), iterable))
        for future in concurrent.futures.as_completed(result_futures):
            try:
                print('resutl is', future.result())
            except Exception as e:
                print('e is', e, type(e))
    # resutl is 3
    # resutl is 1
    # resutl is 4
    # e is x 
    # resutl is 6
    # resutl is 7
    # resutl is 8
    # resutl is 9
    # resutl is 10
    

    in executor.map, if there is an exception, the whole executor would stop. you need to handle the exception in the worker function.

    with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor:
        for each in executor.map(f, iterable):
            print(each)
    # if there is any exception, executor.map would stop
    

提交回复
热议问题