← retour aux snippets

python: concurrent.futures

Pool de threads/process pour IO/CPU.

objectif

Pool de threads/process pour IO/CPU.

code minimal

from concurrent.futures import ThreadPoolExecutor
def f(x): return x+1
with ThreadPoolExecutor(max_workers=2) as ex:
    futs = [ex.submit(f, i) for i in range(3)]
print(sum(f.result() for f in futs))

utilisation

from concurrent.futures import ProcessPoolExecutor
def g(x): return x*x
with ProcessPoolExecutor(max_workers=2) as ex:
    res = list(ex.map(g, [1,2,3]))
print(res[0])

variante(s) utile(s)

from concurrent.futures import as_completed, ThreadPoolExecutor
with ThreadPoolExecutor(max_workers=2) as ex:
    its = [ex.submit(lambda v=v: v, i) for i in range(3)]
    done = [f.result() for f in as_completed(its)]
print(len(done))

notes

  • Threads pour IO; Process pour CPU lié, GIL oblige.