Skip to content

Instantly share code, notes, and snippets.

@yue4u
Created August 28, 2019 18:55
Show Gist options
  • Select an option

  • Save yue4u/c50d54e824072f8e105a4c561343a806 to your computer and use it in GitHub Desktop.

Select an option

Save yue4u/c50d54e824072f8e105a4c561343a806 to your computer and use it in GitHub Desktop.
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