Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- from concurrent import futures
- from time import sleep
- def count(name, target):
- """ Counts from 1 to target integer """
- for i in range(1, target + 1):
- sleep(1)
- return print(name + " - Success")
- with futures.ThreadPoolExecutor(max_workers=20) as executor:
- count_jobs = [3, 4, 1, "abc", 7, 2, 6, 5]
- # Builds a dictionary, key=(futures object) value=(item from list)
- threads = {executor.submit(count, "count_for_{}".format(str(job)), job): job for job in count_jobs}
- for future in futures.as_completed(threads):
- try:
- future.result()
- except Exception as err:
- print("!!! ERROR {}: {} !!!".format("count_for_{}".format(threads[future]), err))
- # Comment this to ignore errors
- raise
Add Comment
Please, Sign In to add comment