-
-
Save iancze/8995329 to your computer and use it in GitHub Desktop.
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 time | |
import multiprocessing as mp | |
import gc | |
#From https://gist.github.com/astrofrog/1453933 | |
import numpy as np | |
import matplotlib.pyplot as plt | |
class Plotter(): | |
def __init__(self, processes=mp.cpu_count()): | |
self.manager = mp.Manager() | |
self.nc = self.manager.Value('i', 0) | |
self.pids = [] | |
self.processes = processes | |
def async_plotter(self, nc, fig, filename, processes): | |
while nc.value >= processes: | |
time.sleep(0.1) | |
nc.value += 1 | |
print("Plotting " + filename) | |
fig.savefig(filename) | |
#plt.close(fig) | |
nc.value -= 1 | |
plt.close("all") | |
def save(self, fig, filename): | |
p = mp.Process(target=self.async_plotter, | |
args=(self.nc, fig, filename, self.processes)) | |
p.start() | |
self.pids.append(p) | |
def join(self): | |
for p in self.pids: | |
p.join() | |
self.pids = [] | |
gc.collect() | |
if __name__=="__main__": | |
# Create instance of Asynchronous plotter | |
a = Plotter() | |
for i in range(10): | |
print('Preparing %04i.png' % i) | |
# Generate random points | |
x = np.random.random(10000) | |
y = np.random.random(10000) | |
# Generate figure | |
fig = plt.figure() | |
ax = fig.add_subplot(1, 1, 1) | |
ax.set_xscale('log') | |
ax.set_yscale('log') | |
ax.scatter(x, y) | |
# Add figure to queue | |
a.save(fig, '%04i.png' % i) | |
# Wait for all plots to finish | |
a.join() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment