Executor is a class concurrent.futures.Executor
which is used of offload function calls asynchronously. It immediately returns a future object which can be used to get the result.
def blocking_io(label: int, bytes: int):
time.sleep(4)
print(f'--------> FOR {label} <---------')
with open('/usr/share/dict/words', 'r') as f:
print(f.read(bytes))
print('-----------------------------')
executor = concurrent.futures.ThreadPoolExecutor()
handler = executor.submit(blocking_io, 1, 100)
handler.add_done_callback(
lambda future: print(future))
There are different Executor available in concurrent.futures
such as ThreadPoolExecutor
to execute tasks in Threads pool.
We also have ProcessPoolExecutor
for cpu bound tasks.