Skip to content

Instantly share code, notes, and snippets.

@tristanwietsma
Last active August 29, 2015 13:57
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 tristanwietsma/9841064 to your computer and use it in GitHub Desktop.
Save tristanwietsma/9841064 to your computer and use it in GitHub Desktop.
parallel python examples
import random
from joblib import Parallel, delayed
import time
def Example(c):
some_random_delay = int(2*random.random()*c)
time.sleep(some_random_delay) # let's pretend we're doing work
print some_random_delay
return some_random_delay
num_cores = 4
jobs = Parallel(n_jobs=num_cores)(delayed(Example)(i) for i in range(10))
print jobs
import random
import time
from concurrent.futures import ThreadPoolExecutor
def Example(c):
some_random_delay = int(2*random.random()*c)
time.sleep(some_random_delay) # let's pretend we're doing work
print(some_random_delay)
return some_random_delay
jobs = []
num_cores = 4
with ThreadPoolExecutor(max_workers=num_cores) as queue:
for i in range(10):
jobs.append(queue.submit(Example, i))
print([j.result() for j in jobs])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment