Dec-06-2019, 06:04 PM
(This post was last modified: Dec-06-2019, 06:05 PM by DreamingInsanity.)
I've got it working with multiprocessing's queue class:
def create_averaging_process(processes, image_file, q): p = Process(target=get_averages, args=(image_file, q)) processes.append(p) p.start() def write_to_file(file, q, end): with open(file, 'a') as f: while True: line = q.get() if line == end: return f.write(str(line)) def get_averages(image, q): try: img = Image.open(image).convert('RGB') except Exception as e: print(e) img2 = img.resize((1, 1), Image.ANTIALIAS) #easiest way of getting average color - resize to 1x1 with anti-alias col = img2.getpixel((0, 0)) #get color of that single pixel try: something = col[2] #sometimes there are just singular ints q.put(col) except Exception as e: print(e) if __name__ == "__main__": index = 0 processes = [] queue = multiprocessing.Queue() STOP_TOKEN="end" allfiles = glob.glob(path+"/images/*") while (len(processes)<len(allfiles)): if (len(processes) - len([p for p in processes if not p.is_alive()]) < MAX_PROCESSES): create_averaging_process(processes, allfiles[index], queue) index+=1 writer_process = multiprocessing.Process(target = write_to_file, args=(path+"/test.txt", queue, STOP_TOKEN)) writer_process.start() queue.put(STOP_TOKEN) writer_process.join()