-
-
Save yue4u/c50d54e824072f8e105a4c561343a806 to your computer and use it in GitHub Desktop.
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 threading import Thread | |
| import random | |
| import time | |
| tasks = [f'task-{num}' for num in range(1,5)] | |
| def do_task(t): | |
| def try_do_task(): | |
| time.sleep(1) | |
| if random.random() > 0.2: | |
| print(f'{t} Done\n') | |
| else: | |
| print(f'{t} Failed, Restarting\n') | |
| raise IOError | |
| while True: | |
| try: | |
| try_do_task() | |
| break | |
| except: | |
| continue | |
| threads = [ | |
| Thread(target=do_task, args=(task,)) | |
| for task in tasks | |
| ] | |
| for thread in threads: | |
| thread.start() | |
| for thread in threads: | |
| thread.join() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment