Slide 15
Slide 15 text
Future
A read only ref to a value that may not
exist (“future” value)
Non-blocking by default and make use
of callbacks
This allows for fast asynchronous
code
PEP 3148 - futures in stdlib (Accepted)
They revolve around some Execution
context - (threads pool, process pool,
main thread**)
Future can be in two states complete/
incomplete - onComplete / fail/
success
import asyncio
tasks = [67, 45, 67, 78, 56]
def factorial(number):
...
executor = futures.ThreadPoolExecutor(max_workers = 10)
# list of futures
future_to_task = dict((executor.submit(factorial, number),
number, for number in tasks)
for future in futures.as_completed(future_to_task):
task = future_to_task[future]
if future.exception() is not None:
print('%r generated an exception: %s' % (task,
future.exception()))
else:
print('task fact({}) result is {} '.format(task,
future.result()))