Last active
August 29, 2015 13:57
-
-
Save tristanwietsma/9841064 to your computer and use it in GitHub Desktop.
parallel python examples
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
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 |
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
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