Skip to content

Instantly share code, notes, and snippets.

@donatello
Created July 25, 2017 09:53
Show Gist options
  • Save donatello/9b6954d74cd60356507bd5ea44eaa396 to your computer and use it in GitHub Desktop.
Save donatello/9b6954d74cd60356507bd5ea44eaa396 to your computer and use it in GitHub Desktop.
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