Last active
March 12, 2020 20:07
-
-
Save tanimislam/413a2a8a02d0dbfa4ebd48500f56cc44 to your computer and use it in GitHub Desktop.
description of something that creates nonsensical images when using Python's multiprocessing framework
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
*.png |
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
#!/usr/bin/env python3 | |
""" | |
This demonstrates that a bunch of images I create here look ruined when I run with multiprocessing, but look fine when run in parallel. | |
serial 100 images: run ./demonstration_borked_images.py -N 100 | |
parallel 100 images: run ./demonstration_borked_images.py -N 100 -M | |
""" | |
import os, sys, numpy, glob, time | |
from multiprocessing import Pool, cpu_count | |
from matplotlib.backends.backend_agg import FigureCanvasAgg | |
from matplotlib.figure import Figure | |
from argparse import ArgumentParser | |
def create_demo_image( imageno ): | |
assert( isinstance( imageno, int ) ) | |
assert( imageno >= 0 ) | |
fig = Figure( figsize = ( 8 * 1.5, 6 * 1.5 ) ) | |
ax = fig.add_subplot(111) | |
xyvals = numpy.random.random_sample( ( 300, 3 ) ) | |
ax.scatter( xyvals[:,0], xyvals[:,1], c = xyvals[:,2], s = 20 ) | |
ax.set_xlabel( 'X VALUES', fontsize = 14, fontweight = 'bold' ) | |
ax.set_ylabel( 'Y VALUES', fontsize = 14, fontweight = 'bold' ) | |
ax.set_title( 'IMAGE NUMBER %04d' % imageno ) | |
canvas = FigureCanvasAgg(fig) | |
canvas.print_figure( 'figure_%04d.png' % imageno, bbox_inches = 'tight' ) | |
if __name__ == '__main__': | |
parser = ArgumentParser( ) | |
parser.add_argument( '-N', '--num', type=int, dest='num', action='store', default = 10, | |
help = 'Number of images to draw. Default is 10.' ) | |
parser.add_argument( '-M', dest='do_multi', action='store_true', default = False, | |
help = ' '.join( | |
[ | |
'If chosen, then create these images in parallel using multiprocessing.', | |
'Default is to create images in serial.' ])) | |
args = parser.parse_args( ) | |
assert( args.num > 0 ) | |
time0 = time.time( ) | |
if not args.do_multi: | |
_ = list( map( create_demo_image, range( args.num ) ) ) | |
else: | |
with Pool( processes = cpu_count( ) ) as pool: | |
_ = list( pool.map( create_demo_image, range( args.num ) ) ) | |
# | |
## how long to run | |
print( 'took %0.3f seconds to process %02d images.' % ( | |
time.time( ) - time0, args.num ) ) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment