Created
September 1, 2025 01:20
-
-
Save rootVIII/bb97bb120b5783a71f6f05492bfeb43a to your computer and use it in GitHub Desktop.
Python example thread pool executor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from concurrent.futures import ThreadPoolExecutor, as_completed | |
| from random import randint | |
| from time import sleep | |
| def task(): | |
| sleep_time = randint(1, 10) | |
| print(f'sleeping {sleep_time}') | |
| sleep(sleep_time) | |
| return 5 * 5 | |
| def task_master(arr): | |
| total = 0 | |
| with ThreadPoolExecutor() as executor: | |
| for future in as_completed([executor.submit(func) for func in arr]): | |
| total += future.result() | |
| return total | |
| if __name__ == '__main__': | |
| res = task_master([task, task, task, task]) | |
| print(res) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment