Last active
October 11, 2015 12:48
-
-
Save clayrichardson/3861372 to your computer and use it in GitHub Desktop.
Create jpegs with random content using multiple 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
import sys | |
import os | |
import time | |
import uuid | |
import random | |
import signal | |
import multiprocessing | |
import numpy | |
from optparse import OptionParser | |
from PIL import Image | |
processes = [] | |
kill_queue = multiprocessing.Queue() | |
def launch_process(number, kill_queue, child_connection, args): | |
signal.signal(signal.SIGINT, signal.SIG_IGN) | |
kill_flag = False | |
path = args['path'] | |
size = args['size'] | |
while not kill_flag: | |
x = random.choice(range(1,size)) | |
y = random.choice(range(1,size)) | |
a = numpy.random.rand(x,y,3) * 255 | |
image_out = Image.fromarray(a.astype('uint8')).convert('RGBA') | |
file_name = os.path.join(path, '%s.%s' % (str(uuid.uuid4()), 'jpg')) | |
image_out.save(file_name) | |
print '%s: Saved %s %s x %s' % (number, file_name, x, y) | |
if not kill_queue.empty(): | |
kill_flag = kill_queue.get() | |
print '%s: Got %s from the queue.' % (number, kill_flag) | |
return | |
def signal_handler(signal, frame): | |
print '\nCaught interrupt, cleaning up...' | |
for process in processes: | |
kill_queue.put(True) | |
sys.exit(0) | |
signal.signal(signal.SIGINT, signal_handler) | |
if __name__ == '__main__': | |
parser = OptionParser() | |
parser.add_option('-p', '--path', dest='path') | |
parser.add_option('-c', '--concurrency', dest='concurrency') | |
parser.add_option('-s', '--size', dest='size') | |
(options, args) = parser.parse_args() | |
path = os.path.abspath(options.path) | |
concurrency = int(options.concurrency) | |
size = int(options.size) | |
parent_connections = [] | |
args = {} | |
args['path'] = path | |
args['size'] = size | |
for number in range(concurrency): | |
parent_connection, child_connection = multiprocessing.Pipe() | |
parent_connections.append(parent_connection) | |
process = multiprocessing.Process(target=launch_process, args=(number,kill_queue,child_connection,args,)) | |
process.start() | |
processes.append(process) | |
while(1): | |
print 'Main process sleeping...' | |
time.sleep(1) | |
print 'Main thread done.' |
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
Pillow==2.0.0 | |
numpy==1.7.1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment