fork download
  1. from concurrent.futures import ThreadPoolExecutor, as_completed
  2.  
  3. def fib(n):
  4. a, b = 0, 1
  5. for i in range(0, n):
  6. a, b = b, a + b
  7. return ((n, a))
  8.  
  9. numeros = [10, 20, 25]
  10.  
  11. with ThreadPoolExecutor(max_workers=5) as executor:
  12. fibSubmit = {executor.submit(fib, n,): n for n in numeros}
  13.  
  14. for future in as_completed(fibSubmit):
  15. try:
  16. n, f = future.result()
  17. except Exception as exc:
  18. print("Erro! {0}".format(exc))
  19. else:
  20. print ("{0}: {1}".format(n, f))
Success #stdin #stdout 0.09s 42080KB
stdin
Standard input is empty
stdout
20: 6765
25: 75025
10: 55