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
def encode_rle(chunk):
return [[len(list(group)), key] for key, group in groupby(chunk)]
def get_file_symbols(path):
with open(path, 'rb') as r:
original= bytearray(r.read())
return [x for x in original]
file_path = "justin.jpg"
img = Image.open(file_path)
img
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
data = get_file_symbols("justin.jpg")
results = init_threads(data, 1000)