Python Glossary Русский

Python 3.2+concurrent.futuresfunction

concurrent.futures.wait()

task pool

Waits on a set of futures until the return_when condition (all, first, first error) or a timeout and returns a pair of sets (done, not_done).

Syntax

done, not_done = concurrent.futures.wait(fs, timeout=None, return_when=ALL_COMPLETED)

It returns sets, so the order of tasks is lost — keep your list of futures if order matters. wait does not raise task errors: they wait inside result(). A timeout raises no TimeoutError, unlike as_completed — you just get a non-empty not_done, and the unfinished tasks are not cancelled and keep running.

Examples

import time
from concurrent.futures import ThreadPoolExecutor, wait, FIRST_COMPLETED
with ThreadPoolExecutor() as ex:
    fast = ex.submit(time.sleep, 0.01)
    slow = ex.submit(time.sleep, 0.4)
    done, not_done = wait([fast, slow], return_when=FIRST_COMPLETED)
    print(fast in done, slow in not_done)  # → True True
from concurrent.futures import ThreadPoolExecutor, wait
with ThreadPoolExecutor() as ex:
    fs = [ex.submit(int, s) for s in ['1', 'x']]
    done, _ = wait(fs)  # ошибки здесь не всплывают
    print(len(done))  # → 2
    print(sum(f.exception() is not None for f in fs))  # → 1

See also

Open in the full glossarydocs.python.orgReport an inaccuracy