Skip to content

Instantly share code, notes, and snippets.

@krystophny
Created May 14, 2019 06:58
Show Gist options
  • Save krystophny/4c7a46ff835d845fe75be45e68391b8f to your computer and use it in GitHub Desktop.
Save krystophny/4c7a46ff835d845fe75be45e68391b8f to your computer and use it in GitHub Desktop.
Exporting plots in parallel using concurrent.futures
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