[Python] How to keep the original order of input when using ThreadPoolExecutor?

Discussão em 'Python' iniciado por Stack, Dezembro 21, 2024.

  1. Stack

    Stack Membro Participativo

    from concurrent.futures import ThreadPoolExecutor, as_completed

    def add_one(number, n):
    return number + 1 + n

    def process():
    all_numbers = []
    for i in range(0, 10):
    all_numbers.append(i)

    threads = []
    all_results = []
    with ThreadPoolExecutor(max_workers=10) as executor:
    for number in all_numbers:
    threads.append(executor.submit(add_one, number))

    for index, task in enumerate(as_completed(threads)):
    result = task.result()
    #print(result)
    all_results.append(result)

    for index, result in enumerate(all_results):
    print(result)

    process()


    If I set max_works=1, it will print out from 1 to 10 in order; if I set max_workers = 10, the order could be random:

    5
    3
    10
    7
    1
    8
    6
    2
    4
    9


    How to keep the original order of the input when using ThreadPoolExecutor to process a list of items as in this example?

    Continue reading...

Compartilhe esta Página