Created
August 27, 2019 18:17
-
-
Save ssbarnea/570668b18fa539e59caf4c882fe7348a to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env python | |
from __future__ import print_function | |
# for python2.7 do `pip install futures` | |
from concurrent import futures | |
import sys | |
from time import sleep | |
from random import randint | |
def worker(i): | |
result = randint(0, 10) | |
sleep(result/10) | |
if result == 1: | |
raise NameError("foo") | |
elif result == 2: | |
return False | |
return True | |
workers = 5 | |
tasks = range(20) | |
print("INFO: Starting %s tasks..." % len(tasks), file=sys.stderr) | |
failures = 0 | |
with futures.ThreadPoolExecutor(max_workers=workers) as executor: | |
future_list = [] | |
for task in tasks: | |
future = executor.submit(worker, task) | |
future_list.append(future) | |
try: | |
r = future.result(timeout=0.8) | |
print("INFO: Task %s returned %s" % (task, r), file=sys.stderr) | |
except futures._base.TimeoutError: | |
# that exception contains no other info than its name | |
print("ERROR: TimeoutError while running task %s" % task, file=sys.stderr) | |
failures += 1 | |
pass | |
except Exception as e: | |
print("ERROR: %s raised by task %s: %s" % (type(e).__name__, task, e), file=sys.stderr) | |
failures += 1 | |
if failures: | |
sys.stderr.flush() | |
print("ERROR: Finished with %s/%s task failures." % (failures ,len(tasks)), file=sys.stderr) | |
sys.exit(66) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment