Created
May 14, 2019 06:58
-
-
Save krystophny/4c7a46ff835d845fe75be45e68391b8f to your computer and use it in GitHub Desktop.
Exporting plots in parallel using concurrent.futures
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 concurrent.futures | |
import matplotlib.pyplot as plt | |
import numpy as np | |
import time, random | |
def do_stuff(a): # these are examples. | |
x = np.linspace(1,10) | |
plt.plot(x, np.cos(a*x)) | |
plt.savefig('{}.png'.format(a)) | |
time.sleep(random.random()) | |
print(a) | |
return a | |
output = list() | |
alist = [1,2,3,4,5,6,7,8,9] # a = 1, 2, 3 | |
with concurrent.futures.ProcessPoolExecutor() as executor: | |
for out in executor.map(do_stuff, alist): | |
output.append(out) | |
print(output) # map maps correct output independent of process order |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment