Python 3.2+concurrent.futuresfunction
concurrent.futures.Executor.submit()
task pool
Sends a call to the pool for execution and immediately returns a Future object, without waiting for the result.
Syntax
future = executor.submit(fn, *args, **kwargs)
The classic slip is ex.submit(fn(x)): that calls the function right there and hands the pool a finished value. Arguments go separately: ex.submit(fn, x). Also, submit never raises the task's exception — it is stored in the Future and re-raised by result(), so a pool whose results nobody collects swallows failures silently.
Examples
from concurrent.futures import ThreadPoolExecutor
def greet(name):
return f'Привет, {name}!'
with ThreadPoolExecutor() as ex:
f1 = ex.submit(greet, 'Артём')
f2 = ex.submit(greet, 'Python')
print(f1.result()) # Привет, Артём!
print(f2.result())
See also
Open in the full glossarydocs.python.orgReport an inaccuracy