Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bf5a0a4

Browse files
committed
Improve speed of gallery thumbnail generation using multiprocessing
svn path=/trunk/matplotlib/; revision=7759
1 parent 30a945e commit bf5a0a4

1 file changed

Lines changed: 20 additions & 11 deletions

File tree

doc/sphinxext/gen_gallery.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,14 @@ def gen_gallery(app, doctree):
3939
'matplotlib_icon',
4040
])
4141

42-
print
43-
print "generating gallery: ",
4442
data = []
43+
thumbnails = {}
44+
4545
for subdir in ('api', 'pylab_examples', 'mplot3d', 'widgets', 'axes_grid' ):
4646
origdir = os.path.join('build', rootdir, subdir)
4747
thumbdir = os.path.join(outdir, rootdir, subdir, 'thumbnails')
4848
if not os.path.exists(thumbdir):
4949
os.makedirs(thumbdir)
50-
print subdir,
5150

5251
for filename in sorted(glob.glob(os.path.join(origdir, '*.png'))):
5352
if filename.endswith("hires.png"):
@@ -56,16 +55,14 @@ def gen_gallery(app, doctree):
5655
path, filename = os.path.split(filename)
5756
basename, ext = os.path.splitext(filename)
5857
if basename in skips:
59-
sys.stdout.write('[skipping %s]' % basename)
60-
sys.stdout.flush()
6158
continue
6259

6360
# Create thumbnails based on images in tmpdir, and place
6461
# them within the build tree
6562
orig_path = str(os.path.join(origdir, filename))
6663
thumb_path = str(os.path.join(thumbdir, filename))
67-
if out_of_date(orig_path, thumb_path):
68-
image.thumbnail(orig_path, thumb_path, scale=0.3)
64+
if out_of_date(orig_path, thumb_path) or True:
65+
thumbnails[orig_path] = thumb_path
6966

7067
m = multiimage.match(basename)
7168
if m is None:
@@ -77,10 +74,6 @@ def gen_gallery(app, doctree):
7774
data.append((subdir, basename,
7875
os.path.join(rootdir, subdir, 'thumbnails', filename)))
7976

80-
sys.stdout.write(".")
81-
sys.stdout.flush()
82-
print
83-
8477
link_template = """\
8578
<a href="%s"><img src="%s" border="0" alt="%s"/></a>
8679
"""
@@ -99,5 +92,21 @@ def gen_gallery(app, doctree):
9992
fh.write(template%'\n'.join(rows))
10093
fh.close()
10194

95+
try:
96+
import multiprocessing
97+
def make_thumbnail(args):
98+
image.thumbnail(args[0], args[1], 0.3)
99+
100+
app.builder.info("generating thumbnails... ", nonl=True)
101+
pool = multiprocessing.Pool()
102+
pool.map(make_thumbnail, thumbnails.iteritems())
103+
app.builder.info("done")
104+
105+
except ImportError:
106+
for key in app.builder.status_iterator(
107+
thumbnails.iterkeys(), "generating thumbnails... ",
108+
length=len(thumbnails)):
109+
image.thumbnail(key, thumbnails[key], 0.3)
110+
102111
def setup(app):
103112
app.connect('env-updated', gen_gallery)

0 commit comments

Comments
 (0)