Created
July 25, 2017 09:53
-
-
Save donatello/9b6954d74cd60356507bd5ea44eaa396 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
import sys | |
import time | |
_PARALLEL_UPLOADERS = 3 | |
#: Python 2.x? | |
# _is_py2 = (sys.version_info[0] == 2) | |
#: Python 3.x? | |
_is_py3 = (sys.version_info[0] == 3) | |
if _is_py3: | |
from concurrent.futures import ThreadPoolExecutor | |
tp = ThreadPoolExecutor(max_workers=_PARALLEL_UPLOADERS) | |
else: | |
# Python 2 :( | |
from multiprocessing.dummy import Pool | |
tp = Pool(_PARALLEL_UPLOADERS) | |
def f(arg): | |
print(arg) | |
time.sleep(1) | |
return arg | |
def f_exc(arg): | |
print(arg) | |
time.sleep(1) | |
raise ValueError("oops " + str(arg)) | |
return arg | |
results = tp.map(f, range(12)) | |
for r in results: | |
print("Result:", r) | |
print("Running func that raises exceptions: ") | |
try: | |
results = tp.map(f_exc, range(12)) | |
for r in results: | |
print("Result:", r) | |
except ValueError as v: | |
print("caught it!") | |
print(v) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment