Created
April 26, 2018 11:33
-
-
Save timtroendle/0029336764c0db2cb6f2832e08f51789 to your computer and use it in GitHub Desktop.
Prototype of plotting matplotlib plots in consecutive processes.
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
"""Prototype of plotting matplotlib plots in consecutive processes..""" | |
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas | |
from matplotlib.figure import Figure | |
from multiprocessing import Pool | |
# avoid to import pyplot as it'll import (on my machine) backends that fail with multiprocessing. | |
def do_plot(number): | |
fig = Figure(figsize=(13, 7)) | |
FigureCanvas(fig) | |
ax = fig.add_subplot(1, 1, 1) | |
ax.plot([0, 1, 2, 3], [-1, 1, 0, 2]) | |
fig.savefig("yo-plot{}.png".format(number)) | |
if __name__ == '__main__': | |
with Pool(4) as pool: | |
for result in [pool.apply_async(do_plot, (id,)) for id in range(10)]: | |
result.wait() | |
print("All applied.") | |
pool.join() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment