Skip to content

Instantly share code, notes, and snippets.

@gdassori
Created July 30, 2019 16:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gdassori/73259b91aa78b15b65bae70fa7758b31 to your computer and use it in GitHub Desktop.
Save gdassori/73259b91aa78b15b65bae70fa7758b31 to your computer and use it in GitHub Desktop.
python: run_together
def run_together(*tasks) -> Tuple:
from multiprocessing.pool import ThreadPool
pool = ThreadPool(len(tasks) if len(tasks) < 8 else 8)
results = []
for task in tasks:
if len(task) > 3:
raise ValueError('Syntax Error')
if len(task) > 1:
a = task[1] if isinstance(task[1], tuple) else (task[1],)
else:
a = tuple()
k = task[2] if len(task) == 3 else {}
results.append(pool.apply_async(task[0], args=a, kwds=k))
pool.close()
pool.join()
results = [r.get() for r in results]
return tuple(results)
if __name__ == '__main__':
def fprint(b, c=None):
return str(b) + ' ' + str(c) + ' ' + str(time.time())
t = run_together(
(fprint, '1', {'c': 'a'}),
(fprint, 2, {'c': 'b'}),
(fprint, '3', {'c': 'c'}),
(fprint, 4, {'c': 'd'})
)
print(type(t), t)
try:
t = run_together(
(1, 2, 3, 4)
)
except ValueError as e:
print(e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment