Guest User

Untitled

a guest
Dec 12th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. from concurrent import futures
  4. from time import sleep
  5.  
  6.  
  7. def count(name, target):
  8. """ Counts from 1 to target integer """
  9. for i in range(1, target + 1):
  10. sleep(1)
  11. return print(name + " - Success")
  12.  
  13.  
  14. with futures.ThreadPoolExecutor(max_workers=20) as executor:
  15. count_jobs = [3, 4, 1, "abc", 7, 2, 6, 5]
  16. # Builds a dictionary, key=(futures object) value=(item from list)
  17. threads = {executor.submit(count, "count_for_{}".format(str(job)), job): job for job in count_jobs}
  18. for future in futures.as_completed(threads):
  19. try:
  20. future.result()
  21. except Exception as err:
  22. print("!!! ERROR {}: {} !!!".format("count_for_{}".format(threads[future]), err))
  23. # Comment this to ignore errors
  24. raise
Add Comment
Please, Sign In to add comment