Development Exercises – L11.1

Created by Ramses Alexander Coraspe Valdez

Created on May 10, 2020

In [0]:
from collections import Counter
from itertools import chain, groupby, permutations, product 
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
import timeit as tiempo
import multiprocessing as mp
from PIL import Image
In [0]:
def encode_rle(chunk):
    return [[len(list(group)), key] for key, group in groupby(chunk)]
In [0]:
def get_file_symbols(path):
    with open(path, 'rb') as r:                                                           
            original= bytearray(r.read())
    return [x for x in original]
In [77]:
file_path = "justin.jpg"
img = Image.open(file_path)
img
Out[77]:
In [0]:
def execute(d, chunk):     
    result = encode_rle(d) 
    nr= []
    for r in result:
        if(r[0]>1):
            nr.append(r)

    print("Thread processing the chunk %s: reached the barrier ", chunk)
    return (chunk, nr)                        

def init_threads(data, sizechunk):    
    pool = mp.Pool(processes=6)        
    results = [pool.apply_async(execute, args=(data[x:x+sizechunk], x)) for x in range(0, len(data), sizechunk)]

    nr = []
    for p in results:
        nr.append(len(p.get()[1]))
    
    print("All processes reached the barrier")

    return nr
In [0]:
data = get_file_symbols("justin.jpg")
results = init_threads(data, 1000)